1

Here i am sorting a alphanumeric string value using javascript.But it sorts the string alone.What i need is to split the numeric values from the string and sort the numeric values.Here is my code

 function sortUnorderedList(ul, sortDescending) {
        if (typeof ul == "string")
            ul = document.getElementById(ul);

        var lis = ul.getElementsByTagName("li");
        var vals = [];
        for (var i = 0, l = lis.length; i < l; i++)
            vals.push(lis[i].innerHTML);

        vals.sort();

        if (sortDescending)
            vals.reverse();

        for (var i = 0, l = lis.length; i < l; i++)
            lis[i].innerHTML = vals[i];
    }

Any suggestion?

EDIT:Current Result

PPG 101
PPG 102
PPG 57
PPG 58
PPG 99

Expected Result:

PPG 57
PPG 58
PPG 99
PPG 101
PPG 102
Andy
  • 61,948
  • 13
  • 68
  • 95
bala3569
  • 10,832
  • 28
  • 102
  • 146
  • Please provide sample HTML so we can see what you're trying to accomplish. Are you trying to sort the numbers from a list of LI tags while ignoring all non-numbers in the HTML? – jfriend00 Aug 13 '12 at 08:53
  • @jfriend00:No first i need to sort the alphabets after that sort numerics – bala3569 Aug 13 '12 at 08:58
  • http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array – ArtBIT Aug 13 '12 at 09:04

3 Answers3

2

To perform a numeric sort, you must pass a function as an argument when calling the sort method.

vals.sort(function(a, b) { 
    return (a-b);
});

See http://www.javascriptkit.com/javatutors/arraysort.shtml for more details

Julien Lafont
  • 7,869
  • 2
  • 32
  • 52
  • I havn't see the string before the number, use the Mitch Satchwell function bellow which is more complete (it's in the same idea). – Julien Lafont Aug 13 '12 at 09:14
1

If you wish to sort first by the string, and then by the numeric value after the space, you will need to specify your own function to pass in to the sort function, like so:

arr.sort(function(a,b) { 
  a = a.split(' ');
  b = b.split(' ');
  if (a[0] == b[0]) {
    return parseInt(a[1]) - parseInt(b[1]);
  }
  return a[0] > b[0];
});

The above example sorts first by the string and then by the numbers after the string (numerically). The above code works on the assumption values being sorted will always be in the above format and lacks any checking on the input.

Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
  • on first click it is working fine...but on second click showing error Microsoft JScript runtime error: '0' is null or not an object – bala3569 Aug 13 '12 at 10:23
0
function sort(values) {
    var y = values.sort(function (a, b) {
        var x = a.split(' ');
        var y = a.split(' ');
        if (parseInt(x[1]) > parseInt(y[1])) {
            return a > b;
        }
        return a < b;
    });
    return y;
}
  • please elaborate – JJJ Apr 21 '19 at 18:24
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CertainPerformance Apr 21 '19 at 19:47