Is there a library or cross-browser native implementation to sort an array of Danish strings alphabetically in JavaScript?
[Aalborg, Sorø ...]
Unfortunately #Tibos solution doesnt work. The danish letters æøå is not sortable as a dane would expect. And it certainly wont work with aa, which is considered as an oldschool å. The only solution is to make an "handheld" sort-algorithm.
Here is working solution :
arr.sort(function(a,b) {
function getCode(c) {
c=c.toLowerCase();
if (c.substring(0,2)=='aa') return 300;
switch (c.charCodeAt(0)) {
case 229 : //å
return 299;
break;
case 248 : //ø
return 298;
break;
case 230 : //æ
return 297;
break;
default :
return c.charCodeAt(0);
break;
}
}
return getCode(a) - getCode(b);
});
The test array
var arr = ['Ølby', 'Ålestrup', 'Ærø', 'Almindingen', 'Aalborg', 'Sorø'];
is by locale sorted as
["Ølby", "Ærø", "Ålestrup", "Sorø", "Almindingen", "Aalborg"]
Which is totally wrong. The above function sort the array correct :
["Almindingen", "Sorø", "Ærø", "Ølby", "Ålestrup", "Aalborg"]
Update
#tibos was absolutely right. The above algorithm just sort by the first letter. The below function converts the strings to array of integers, according to the sorting-scheme from the algorithm above. Then it compares the integer arrays - by that, the strings are sorted in their full length :
arr.sort(function(a,b) {
var d, e, f;
function getIntArray(c) {
var array=[];
c=c.toLowerCase();
for (var i=0;i<c.length;i++) {
if (c.substring(i,2)=='aa') {
array.push(300);
i++;
} else {
switch (c.charCodeAt(i)) {
case 229 : //å
array.push(299);
break;
case 248 : //ø
array.push(298);
break;
case 230 : //æ
array.push(297);
break;
default :
array.push(c.charCodeAt(i));
break;
}
}
}
return array;
}
d=getIntArray(a);
e=getIntArray(b);
for (f=0;f<d.length;f++) {
if (d[f]!=e[f]) {
return d[f] - e[f];
}
}
});
test array :
var arr = ['Ølby', 'Ålborg', 'Århus', 'Ålestrup', 'Åkikrkeby', 'Ærø', 'Almindingen', 'Aalborg', 'Sorø'];
is now sorted in full length :
["Almindingen", "Sorø", "Ærø", "Ølby", "Åkikrkeby", "Ålborg", "Ålestrup", "Århus", "Aalborg"]
You can use the following method to sort strings in any language in Chrome and IE11:
var arr = ['Aalborg', 'Sorø']; // array to sort
var myLocale = 'da-DK'; // danish locale
var sortedArr = arr.sort(function(a,b) { return a.localeCompare(b, myLocale); }); // sort
console.log(sortedArr);
For a more browser agnostinc solution, you have two options:
shim the localeCompare function (or replace it with one only for danish)
change the whole sorting algorithm
For this task i would use a bucket-sort-like algorithm that should in theory run faster than the default sort (it would make the minimal number of comparisons).
The general idea is that you go through each string, place them in (sorted) buckets according to the first letter. You continue to split each bucket containing at least 2 strings according to the second letter, then the third and so on. At the end you merge the buckets and you have the sorted array.
Related
I'm starting with react-native building an app to track lap times from my RC Cars. I have an arduino with TCP connection (server) and for each lap, this arduino sends the current time/lap for all connected clients like this:
{"tx_id":33,"last_time":123456,"lap":612}
In my program (in react-native), I have one state called dados with this struct:
dados[tx_id] = {
tx_id: <tx_id>,
last_time:,
best_lap:0,
best_time:0,
diff:0,
laps:[]
};
This program connects to arduino and when receive some data, just push to this state. More specific in laps array of each transponder. Finally, I get something like this:
dados[33] = {
tx_id:33,
last_time: 456,
best_lap: 3455,
best_time: 32432,
diff: 32,
laps: [{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332}]
}
dados[34] = {
tx_id:34,
last_time: 123,
best_lap: 32234,
best_time: 335343,
diff: 10,
laps: [{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332}]
}
dados[35] = {
tx_id:35,
last_time: 789,
best_lap: 32234,
best_time: 335343,
diff: 8,
laps: [{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332},{lap:4,time:343232}]
}
This data in rendered to View's using map function (not a FlatList).
My problem now is that I need to order this before printing on screen.
Now, with this code, data are printed using tx_id as order, since it's the key for main array. Is there a way to order this array using number of elements in laps property and the second option to sort, use last_time property of element?
In this case, the last tx of my example (35) would be the first in the list because it has one lap more than other elements. The second item would be 34 (because of last_time). And the third would be tx 33.
Is there any way to to this in JavaScript, or I need to create a custom functions and check every item in recursive way?!
Tks #crackhead420
While waiting for reply to this question, I just found what you said.... :)
This is my final teste/solution that worked:
var t_teste = this.state.teste;
t_teste[33] = {tx_id: 33, last_time:998,best_lap:2,best_time:123,diff:0,laps:[{lap:1,time:123},{lap:2,time:456}]};
t_teste[34] = {tx_id: 34, last_time:123,best_lap:2,best_time:123,diff:0,laps:[{lap:1,time:123},{lap:2,time:456}]};
t_teste[35] = {tx_id: 35, last_time:456,best_lap:2,best_time:123,diff:0,laps:[{lap:1,time:123},{lap:2,time:456},{lap:3,time:423}]};
t_teste[36] = {tx_id: 36, last_time:789,best_lap:2,best_time:123,diff:0,laps:[{lap:1,time:123},{lap:2,time:456}]};
console.log('Teste original: ',JSON.stringify(t_teste));
var saida = t_teste.sort(function(a, b) {
if (a.laps.length > b.laps.length) {
return -1;
}
if (a.laps.length < b.laps.length) {
return 1;
}
// In this case, the laps are equal....so let's check last_time
if (a.last_time < b.last_time) {
return -1; // fastest lap (less time) first!
}
if (a.last_time > b.last_time) {
return 1;
}
// Return the same
return 0;
});
console.log('Teste novo: ',JSON.stringify(saida));
Using some simple helper functions, this is definitely possible:
const data = [{tx_id:33,last_time:456,best_lap:3455,best_time:32432,diff:32,laps:[{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332}]},{tx_id:34,last_time:123,best_lap:32234,best_time:335343,diff:10,laps:[{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332}]},{tx_id:35,last_time:789,best_lap:32234,best_time:335343,diff:8,laps:[{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332},{lap:4,time:343232}]}]
const sortBy = fn => (a, b) => -(fn(a) < fn(b)) || +(fn(a) > fn(b))
const sortByLapsLength = sortBy(o => o.laps.length)
const sortByLastTime = sortBy(o => o.last_time)
const sortFn = (a, b) => -sortByLapsLength(a, b) || sortByLastTime(a, b)
data.sort(sortFn)
// show new order of `tx_id`s
console.log(data.map(o => o.tx_id))
sortBy() (more explanation at the link) accepts a function that selects a value as the sorting criteria of a given object. This value must be a string or a number. sortBy() then returns a function that, given two objects, will sort them in ascending order when passed to Array.prototype.sort(). sortFn() uses two of these functions with a logical OR || operator to employ short-circuiting behavior and sort first by laps.length (in descending order, thus the negation -), and then by last_time if two objects' laps.length are equal.
Its possible to sort an object array by theire values:
dados.sort(function(a, b) {
return a.last_time - b.last_time;
});
This question is an extension of this one: Checking containment in set of lists in javascript. I want to be able to use a set like function in nodejs or Javascript that can support checking whether or not a list belongs to a collection. For example, given the example in the link, I would like the behavior:
var s = new SetWithListCheckingAbility([[1,2], [2,3]])
s.has([2, 3])
true
I was unable to find any nodejs library that has this functionality, however. The other obvious solution seems to be JSON serializing each object that is added to the set object, and doing checking based on the JSON string, since Javascript equality works for strings. This would probably require subclassing the Set object in ES6. However, I am not sure how to do this for this case...
What you can do is take each member of the set and convert it to a string format (this answer looks like an elegant way to do that conversion from numbers to strings).
For your example, if you want s.has([3, 2]) to return false because [2,3] doesn't count as a match, the array to string conversion would look like array.join(','), otherwise array.sort().join(',') if order doesn't matter.
function setOfListsHasElement(theSet, theElement) {
let newSet = new Set();
theSet.forEach(e => newSet.add(e.join(',')) );
return newSet.has(theElement.join(','));
}
Example usage:
var theSet = new Set();
theSet.add([1,2]);
theSet.add([2,3]);
setOfListsHasElement(theSet, [2,3]); // true
setOfListsHasElement(theSet, [3,2]); // false
setOfListsHasElement(theSet, [2,6]); // false
setOfListsHasElement(theSet, ["1", "2"]); // true - don't know how you want to handle scenarios like this, where the string representation of ["1", "2"] matches that of [1,2]
I figured out how to write a custom class that does what we want:
class SetImproved extends Set{
constructor(){
super();
this.classDict = {};
this._size = 0;
}
get size(){
return this._size
}
add(x){
if(!(JSON.stringify(x) in this.classDict)){
this._size += 1;
}
this.classDict[JSON.stringify(x)] = x;
}
has(x){
return JSON.stringify(x) in this.classDict;
}
delete(x){
if(JSON.stringify(x) in this.classDict){
this._size -= 1;
}
delete this.classDict[JSON.stringify(x)];
}
clear(){
this.classDict = {};
}
keys(){
return Object.keys(this.classDict).map(x => this.classDict[x]);
}
entries(){
return Object.keys(this.classDict).map(x => this.classDict[x]);
}
}
Some examples of the functionality:
var setImproved = new SetImproved()
setImproved.add([1, "b"])
setImproved.add([2, "c"])
setImproved.add(3)
setImproved.add("asdf")
console.log(setImproved.has([1, "b"]))
console.log(setImproved.has([3]))
setImproved.delete([4])
setImproved.delete([1, "b"])
console.log(setImproved.has(3))
console.log(setImproved.entries())
console.log(setImproved.size)
I'm manipulating a lot of numbers in my application. For this particular case, here is what I do : I retrieve two lists of numbers, I do an average for each of these list, then I substract the two average. To avoid average like 3.333333333333 I use .toFixed(3) on my results.
Here is what it looks like :
// I found this function somewhere on Stackoverflow
Array.prototype.average = function() {
if(this.length == 0){
return 0;
}
else{
return this.reduce(function (p, c) {
return p + c;
}) / this.length;
}
};
sumHigh = [ 10.965, 10.889, 10.659, 10.69, 10.599 ]
sumLow = [ 4.807, 3.065, 2.668, 2.906, 3.606, 4.074, 4.153 ]
// Ok normal
console.log(sumHigh.average()) // 10.760399999999999
console.log(sumLow.average()) // 3.6112857142857138
// Ok normal
console.log(sumHigh.average().toFixed(3)) // "10.760" Does the ".." has anything to do with my problem ?
console.log(sumLow.average().toFixed(3)) // "3.611"
// So here I have my two average values with no more than 3 numbers after the comma but it is not taken into account when substracting these two numbers...
// Not Ok, why 10.760 - 3.611 = 7.148999999999999 ?
console.log(sumHigh.average().toFixed(3) - sumLow.average().toFixed(3)) // 7.148999999999999
console.log(parseFloat(sumHigh.average().toFixed(3)) - parseFloat(sumLow.average().toFixed(3))) // 7.148999999999999
// Just as an example, this is working
console.log(parseFloat(sumHigh.average().toFixed(3)) + parseFloat(sumLow.average().toFixed(3))) // 14.371
console.log(parseFloat(sumHigh.average()) + parseFloat(sumLow.average())) // 14.371685714285713
Can someone explain this behaviour?
Why substraction is not working while addition is?
Ok I know I can solve my problem with :
console.log((sumHigh.average() - sumLow.average()).toFixed(3)) // "7.149"
But that doesn't explain this behaviour.
Thanks
I am sorting an array of countries, and each have a data-weight attribute which I use in my custom sort function.
At this point, a_weight and b_weight are either 0 or 1 (integer). 'United States of America' is the only element with a data-weight of 1, every other element has a data-weight of 0.
...
return results.sort(sortOnWeight);
}
function sortOnWeight(a,b) {
a_weight = parseInt($(a['element'][0]['attributes']['data-weight']).val(), 10);
b_weight = parseInt($(b['element'][0]['attributes']['data-weight']).val(), 10);
if (a_weight > b_weight){
return -1;
} else if (a_weight < b_weight) {
return 1;
} else {
return 0;
}
}
The array comes back correct EXCEPT for the last item that hits the 'sortOnWeight' function, which is returned out of order.
For example...result returns as:
United States of America
Mozambique
Aruba
Australia
Austria
Belarus
Anybody see a reason why this could be happening?
It looks like you're banking on the browser sorting algorithm being stable. This is not always the case (see this issue for V8 as an example).
EDIT: More information about V8's sorting here. Apparently, arrays with length <= 10 use a stable sort, while length >= 11 uses a faster unstable sort.
You should sort by text if the weights are equal. String comparison can be done with the > and < operators (MDN reference). The comparison is case-sensitive, with uppercase sorting above lowercase ("abc" > "Abc"), so you may need to use toLowerCase/toUpperCase to fudge your results.
function sortOnWeight(a,b) {
var a_weight = parseInt($(a['element'][0]['attributes']['data-weight']).val(), 10);
var b_weight = parseInt($(b['element'][0]['attributes']['data-weight']).val(), 10);
var a_text;
var b_text;
if (a_weight > b_weight){
return -1;
}
else if (a_weight < b_weight) {
return 1;
}
else {
a_text = /* get text from a */
b_text = /* get text from b */
return a_text > b_text;
}
}
From Mozilla documentation sort is considered unstable:
The sort is not necessarily stable. The default sort order is lexicographic (not numeric).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
This is also mentioned in ES documentation:
The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order).
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11
I just wanted to try it for myself, so created a new 2D array with country names and weights and tried to sort based on weight. It worked to my satisfaction, with US showing up at the top, see the code below which might help you. Of course, this is oversimplified and the links provided by others have a lot more good information and may be necessary depending upon your situation.
<!DOCTYPE html> <html> <body>
<script type = "text/javascript"> var countries = [["Aruba", 0],["Australia", 0],["Austria", 0],["Belarus", 0],["United States of America", 1], ["Mozambique", 0]];
var list = "unsorted :"; for (var i=0;i<countries.length;i++) { list +=countries[i]; list+= " "; }
alert('unsorted array:' + countries);
countries =countries.sort(function(a,b){ return (a[1] > b[1] ? -1 : (a[1] < b[1] ? 1 : 0)); });
list = "sorted :"; for (var i=0;i<countries.length;i++) { list +=countries[i]; list+= " "; }
alert('sorted array:' + countries);
</script>
</body> </html>
I'd like to come up with a good way to have a "suggested" order for how to sort an array in javascript.
So say my first array looks something like this:
['bob','david','steve','darrel','jim']
Now all I care about, is that the sorted results starts out in this order:
['jim','steve','david']
After that, I Want the remaining values to be presented in their original order.
So I would expect the result to be:
['jim','steve','david','bob','darrel']
I have an API that I am communicating with, and I want to present the results important to me in the list at the top. After that, I'd prefer they are just returned in their original order.
If this can be easily accomplished with a javascript framework like jQuery, I'd like to hear about that too. Thanks!
Edit for clarity:
I'd like to assume that the values provided in the array that I want to sort are not guaranteed.
So in the original example, if the provided was:
['bob','steve','darrel','jim']
And I wanted to sort it by:
['jim','steve','david']
Since 'david' isn't in the provided array, I'd like the result to exclude it.
Edit2 for more clarity:
A practical example of what I'm trying to accomplish:
The API will return something looking like:
['Load Average','Memory Usage','Disk Space']
I'd like to present the user with the most important results first, but each of these fields may not always be returned. So I'd like the most important (as determined by the user in some other code), to be displayed first if they are available.
Something like this should work:
var presetOrder = ['jim','steve','david']; // needn't be hardcoded
function sortSpecial(arr) {
var result = [],
i, j;
for (i = 0; i < presetOrder.length; i++)
while (-1 != (j = $.inArray(presetOrder[i], arr)))
result.push(arr.splice(j, 1)[0]);
return result.concat(arr);
}
var sorted = sortSpecial( ['bob','david','steve','darrel','jim'] );
I've allowed for the "special" values appearing more than once in the array being processed, and assumed that duplicates should be kept as long as they're shuffled up to the front in the order defined in presetOrder.
Note: I've used jQuery's $.inArray() rather than Array.indexOf() only because that latter isn't supported by IE until IE9 and you've tagged your question with "jQuery". You could of course use .indexOf() if you don't care about old IE, or if you use a shim.
var important_results = {
// object keys are the important results, values is their order
jim: 1,
steve: 2,
david: 3
};
// results is the orig array from the api
results.sort(function(a,b) {
// If compareFunction(a, b) is less than 0, sort a to a lower index than b.
// See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
var important_a = important_results[a],
important_b = important_results[b],
ret;
if (important_a && !important_b) {ret = -1}
else if (important_b && !important_a) {ret = 1}
else if (important_a && important_b) {ret = important_a - important_b}
else {ret = 0}; // keep original order if neither a or b is important
return(ret);
}
)
Use a sorting function that treats the previously known important results specially--sorts them to the head of the results if present in results.
items in important_results don't have to be in the results
Here's a simple test page:
<html>
<head>
<script language="javascript">
function test()
{
var items = ['bob', 'david', 'steve', 'darrel', 'jim'];
items.sort(function(a,b)
{
var map = {'jim':-3,'steve':-2,'david':-1};
return map[a] - map[b];
});
alert(items.join(','));
}
</script>
</head>
<body>
<button onclick="javascript:test()">Click Me</button>
</body>
</html>
It works in most browsers because javascript typically uses what is called a stable sort algorithm, the defining feature of which is that it preserves the original order of equivalent items. However, I know there have been exceptions. You guarantee stability by using the array index of each remaining item as it's a1/b1 value.
http://tinysort.sjeiti.com/
I think this might help. The $('#yrDiv').tsort({place:'start'}); will add your important list in the start.
You can also sort using this function the way you like.
Live demo ( jsfiddle seems to be down)
http://jsbin.com/eteniz/edit#javascript,html
var priorities=['jim','steve','david'];
var liveData=['bob','david','steve','darrel','jim'];
var output=[],temp=[];
for ( i=0; i<liveData.length; i++){
if( $.inArray( liveData[i], priorities) ==-1){
output.push( liveData[i]);
}else{
temp.push( liveData[i]);
}
}
var temp2=$.grep( priorities, function(name,i){
return $.inArray( name, temp) >-1;
});
output=$.merge( temp2, output);
there can be another way of sorting on order base, also values can be objects to work with
const inputs = ["bob", "david", "steve", "darrel", "jim"].map((val) => ({
val,
}));
const order = ["jim", "steve", "david"];
const vMap = new Map(inputs.map((v) => [v.val, v]));
const sorted = [];
order.forEach((o) => {
if (vMap.has(o)) {
sorted.push(vMap.get(o));
vMap.delete(o);
}
});
const result = sorted.concat(Array.from(vMap.values()));
const plainResult = result.map(({ val }) => val);
Have you considered using Underscore.js? It contains several utilities for manipulating lists like this.
In your case, you could:
Filter the results you want using filter() and store them in a collection.
var priorities = _.filter(['bob','david','steve','darrel','jim'],
function(pName){
if (pName == 'jim' || pName == 'steve' || pName == 'david') return true;
});
Get a copy of the other results using without()
var leftovers = _.without(['bob','david','steve','darrel','jim'], 'jim', 'steve', 'david');
Union the arrays from the previous steps using union()
var finalList = _.union(priorities, leftovers);