How to Sort an Array of Integers Correctly

How to sort an array of integers correctly

By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -

var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
return a - b;
});

console.log(numArray);

How to sort numbers correctly with Array.sort()?

I've tried different numbers, and it always acts as if the 0s aren't there and sorts the numbers correctly otherwise. Anyone know why?

You're getting a lexicographical sort (e.g. convert objects to strings, and sort them in dictionary order), which is the default sort behavior in Javascript:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

array.sort([compareFunction])

Parameters

compareFunction

Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.

In the ECMAscript specification (the normative reference for the generic Javascript), ECMA-262, 3rd ed., section 15.4.4.11, the default sort order is lexicographical, although they don't come out and say it, instead giving the steps for a conceptual sort function that calls the given compare function if necessary, otherwise comparing the arguments when converted to strings:

13. If the argument comparefn is undefined, go to step 16.
14. Call comparefn with arguments x and y.
15. Return Result(14).
16. Call ToString(x).
17. Call ToString(y).
18. If Result(16) < Result(17), return −1.
19. If Result(16) > Result(17), return 1.
20. Return +0.

Very fast way to sort array of integers?

I finally came with a simple and pretty elegant solution :

read N

for (( i=0; i<N; i++ )); do
read tab[i]
done

printf "%i\n" ${tab[@]} | sort -n | awk 'BEGIN{dmin=1000000;} (NR>1){d=$1-prec; if (d<dmin) dmin=d;} {prec=$1;} END{print dmin;}'

And that's it. :)
Thanks to all of you for taking the time to help me ! ;)

How to sort a javascript array of numbers

Usually works for me:

a.sort((a, b) => a - b);

How to sort an array of N elements where each integer belongs to the set {1,2,3,,,k} by using an oracle that answers in yes and no?

Since you know all of the possible values in the array, it suffices to find the frequency of each possible value. Then output the correct number of 1's, the correct number of 2's, etc.

You can achieve this in Theta(k log n) queries, all of the form:

  • "Is the frequency of element x greater than c?"

This amounts to doing a binary search for each of the k frequencies. Since the frequency of each element is an integer in [0, n], you can do this binary search with at most log_2(n+1)+1 queries.

Sort an integer array, keeping first in place

You could save the value of the first element and use it in a condition for the first sorting delta. Then sort by the standard delta.

How it works (the sort order is from Edge)

              condition  numerical     sortFn
a b delta delta result comment
----- ----- --------- --------- --------- -----------------
7 10* 1 1 different section
12* 7 -1 -1 different section
12* 10* 0 2 2 same section
12* 7 -1 -1 same section
3 7 0 -4 -4 same section
3 12* 1 1 different section
3 7 0 -4 -4 same section
5 7 0 -2 -2 same section
5 12* 1 1 different section
5 3 0 2 2 same section
5 7 0 -2 -2 same section
6 7 0 -1 -1 same section
6 3 0 3 3 same section
6 5 0 1 1 same section
6 7 0 -1 -1 same section

* denotes elements who should be in the first section

Elements of different section means one of the elements goes into the first and the other into the second section, the value is taken by the delta of the condition.

Elements of the same section means, both elements belongs to the same section. For sorting the delta of the values is returned.

function sort(array) {

var first = array[0];

array.sort(function (a, b) {

return (a < first) - (b < first) || a - b;

});

return array;

}

console.log(sort([10, 7, 12, 3, 5, 6]));

console.log(sort([12, 8, 5, 9, 6, 10]));
.as-console-wrapper { max-height: 100% !important; top: 0; }


Related Topics



Leave a reply



Submit