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
3
  • 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? Commented Aug 13, 2012 at 8:53
  • @jfriend00:No first i need to sort the alphabets after that sort numerics Commented Aug 13, 2012 at 8:58
  • stackoverflow.com/questions/4340227/… Commented Aug 13, 2012 at 9:04

3 Answers 3

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

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

1 Comment

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).
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.

1 Comment

on first click it is working fine...but on second click showing error Microsoft JScript runtime error: '0' is null or not an object
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;
}

2 Comments

please elaborate
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.