Mastikhor Mastikhor - 6 months ago 14
Javascript Question

How to Sort/Ordering of array list of array that start with the charecter not numeric in JQUERY?

I have an Array :

data =["c3.2xlarge","c3.4xlarge","c3.large"];

it's alphabetical now and sorted, but that gives us when we really want
to appear first.

How do I create a function to sort the objects like this?

  • Array Sort() Method sorts the elements in the array
  • You'll need to tweak the sorting function to get the results you're after

var myArr = ["c3.2xlarge","c3.4xlarge","c3.large"];

//sort by strings
myArr.sort(function(val1, val2){
    return val1.replace(/\D+/g, "") - val2.replace(/\D+/g, "");  

$('#result').html(myArr[0] + ' ' + myArr[1] + ' ' + myArr[2] + '<br>');

// Result : c3.large c3.2xlarge c3.4xlarge arr.sort([compareFunction])