0

I want to sort array like this:

["AAAA1", "AAAA3", "ABAA2", "AAAA10", "AAAA100", "BBB2", "BBB10", "BBAA3", "BBB2AA"]

when i sort it, returns me like:

["AAAA1", "AAAA10", "AAAA100", "AAAA3", "ABAA2", "BBAA3", "BBB10", "BBB2", "BBB2AA"]

That i like to sort it like this:

["AAAA1", "AAAA3", "AAAA10", "AAAA100", "ABAA2", "BBAA3", "BBB2", "BBB2AA", "BBB10"]

so i searched it in stackoverflow and i found something like under links but all of that haven`t general method for sorting characters & numbers string and only works for special questions!!

javascript: sort an array a certain way with integers and characters

How to sort number in javascript sort method

How to sort an array of integers correctly

Edit1:

Please check answers of questions in Sort JavaScript String Array containing numbers, the answer works with text with format var regex = /Value\s([0-9]+)/; and or in Sort mixed alpha/numeric array the answer only works with one character at the beginning of the string.. that i need some method works with all possible string that contains characters and numbers ...!!

Edit2:

Tanx Felix Kling:

i`m so sorry, i have wrong order to array in example, so i liked to BBB2AA order before BBB10

I found the general method for doing that with add leading zero, i wrote a version of it now and please optimize it or give me another options for sorting like that http://jsfiddle.net/Qd8nd/

Community
  • 1
  • 1
Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42
  • 2
    Write a `function mySortFunction(lhs, rhs) { }` that returns `-1` if `lhs` should come before `rhs`, `1` if `rhs` should come before `lhs`, and `0` if they are equal. Call `yourArray.sort(mySortFunction)`. Done. – DCoder Aug 03 '13 at 09:46
  • 1
    possible duplicate of [Sort JavaScript String Array containing numbers](http://stackoverflow.com/questions/3108530/sort-javascript-string-array-containing-numbers) – MightyPork Aug 03 '13 at 09:48
  • 1
    [alphanumeric sort](http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array) – Praveen Aug 03 '13 at 09:49
  • This is called "natural order sorting". – Niet the Dark Absol Aug 03 '13 at 09:50
  • @MightyPork I think you are right. – Sheel Aug 03 '13 at 09:50
  • @MightyPork tanx its only works for "Value\s([0-9]+)" .. check regexp and its not good for me.. i try to write regexp before but i cant get good answer.. – Mehdi Yeganeh Aug 03 '13 at 09:51
  • You can extract both the string part, and the number. Then if string parts are equal, compare the numbers? – MightyPork Aug 03 '13 at 09:52
  • 1
    @Kolink Right. [algorithms for natural order sorting discussed here](http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html) – Praveen Aug 03 '13 at 09:52
  • @user1671639 check the regexp answer works only for character first and then numbers and only works for 1 character at first of string – Mehdi Yeganeh Aug 03 '13 at 09:53
  • @DCoder tanx i know but how can i check equal of two string if i test "aaa10">"aaa2" then i get false that in my case i like to say true – Mehdi Yeganeh Aug 03 '13 at 09:56
  • You have already been told this is called "alphanumeric sort" and given multiple links to it. Read them. – DCoder Aug 03 '13 at 10:00
  • @DCoder tanQ, yep i read it before but i cant write method in javascript to works true, i try to replace numbers with characters or ... but i cant get true method then i asked – Mehdi Yeganeh Aug 03 '13 at 10:06
  • @DCoder please check my edit 2 and give me help for optimize solution or check it for that i wrote it true? and is there better way for doing that? – Mehdi Yeganeh Aug 03 '13 at 12:00

1 Answers1

2

It seems you are treating digits differently, depending on where they appear in the string. If they are between characters, it seems they should be compared alphabetically and if they appear at the end they should be compared numerically. Please correct me if I'm wrong, but otherwise I cannot explain why BBB10 comes before BBB2AA.

There is no "general method" for this kind of problem. You have to implement the logic to compare the values yourself. I.e. you have to split each value in its string and number part and compare the parts individually:

function parseValue(v) {
    // extract number (defaults to 0 if not present)
    var n = +(v.match(/\d+$/) || [0])[0]; 
    var str = v.replace(n, '');  // extract string part
    return [str, n];
}

You can use that function in the .sort callback:

arr.sort(function(a, b) {
    a = parseValue(a);
    b = parseValue(b);

    // compare string part alphabetically
    var result = a[0].localeCompare(b[0]);
    // if the string part is the same, compare the number part
    return result === 0 ? a[1] - b[1] : result;
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • tanQ, i have multiple string and number parts like BBB2AA that i wrote it in question.. so i try to find compare method and i know about sorting in javascript i have problem in comparing and you replace it with `localCompare` please write for me true function for compare it, tanQ again – Mehdi Yeganeh Aug 03 '13 at 10:09
  • The function is in my answer. What is it that you don't understand? – Felix Kling Aug 03 '13 at 10:11
  • i found the answer with your methods, we have general method for doing that only need to add leading zero to numbers behind string, please check http://jsfiddle.net/Qd8nd/ .. please change your asnwer and edit it for accept it. – Mehdi Yeganeh Aug 03 '13 at 11:42
  • The resulting order in your fiddle is different. `BBB2AA` now comes before `BBB10`, unlike in your question. What do you actually want? If you don't formulate your problem properly we cannot help you. – Felix Kling Aug 03 '13 at 11:49
  • i`m sorry.. you right, now i saw .. i have wrong order to array that i need it.. i like to BBB2AA order before BBB10.. and i fixed my question – Mehdi Yeganeh Aug 03 '13 at 11:53