1

I have an array that contains numbers and strings:

disorderedArray = ["74783 Banana", "38903 Orange", "94859 Apple"];

I needed to put them in ascending order by number. I found an example that worked well for descending Sort Alphanumeric String Descending However, I can't seem to change the return line to make the array ascending. I tried to put arr.reverse(); after the return line, but that seemed kind of hackish and it didn't work anyways. I also changed the > and < symbols in the return line, but I started to get crazy results.

function sort() {
  var arr=disorderedArray;
  arr.sort(function(a,b){
    a=a.split(" ");
    b=b.split(" ");
    var an=parseInt(a[0],10);
    var bn=parseInt(b[0],10);
    return an<bn?1:(an>bn?-1:(a[1]<b[1]?-1:(a[1]>b[1]?1:0)));

    arr.reverse(); 

  });
  console.log(arr);
}   
4
  • 1
    arr.reverse() will never be hit as you're using return before it. Commented Dec 19, 2014 at 10:44
  • possible duplicate (stackoverflow.com/a/2802489/2749470) use fu link(davekoelle.com/files/alphanum.js) Commented Dec 19, 2014 at 10:47
  • @aduch: The default sort compares the values as text, so for example 9 abc would come after 177723773 abc. Commented Dec 19, 2014 at 10:52
  • @aduch If all numbers are 5 digits long yes, else no. Commented Dec 19, 2014 at 10:52

2 Answers 2

3

The callback function returns positive or negative values depending on the comparison of the values. Just change the sign of the -1 and 1 values that are returned:

return an<bn?-1:(an>bn?1:(a[1]<b[1]?1:(a[1]>b[1]?-1:0)));

Here is a more readable way to write the same:

return (
  an < bn ? -1 :
  an > bn ? 1 :
  a[1] < b[1] ? 1 :
  a[1] > b[1] ? -1 :
  0
);

Note that the original code sorts the numeric part descending and the rest of the string ascending, so this does the opposite. The value from the first two comparisons determine how the numeric part is sorted, and the last two determine how the rest of the strings are sorted, so you can adjust them to get the desired combination.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked great. Thanks. I knew it had to do with reversing one of the symbols in the return line, but I highly doubt I would have figured that out through trial and error.
How would I change the order of the words from a to z instead of z to a. For example, with the code you gave it puts the alphanumeric array like: ["111 zucchini", "111 orange", "111 apple"]. How would I do: ["111 apple", "111 orange", "111 zucchini"]
@user3080392: You would swap the 1 and -1 in the last comarisons; where a[1] and b[1] are compared.
2

sort's function is just to sort. You need to call reverse on the sorted array.

function sort() {
    var arr = disorderedArray;
    arr.sort(function(a, b) {
        a = a.split(" ");
        b = b.split(" ");
        var an = parseInt(a[0], 10);
        var bn = parseInt(b[0], 10);
        return an < bn ? 1 : (an > bn ? -1 : (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0)));
    });
    console.log(arr.reverse());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.