0
$.ajax({
    url: '/seatlt',
    type: 'POST',
    dataType: 'JSON',
    data: data,
    async: true,
    crossDomain: true,
    success: function(res) {
        $.each(res, function(i, v) {
            $.each(v, function(j, w) {
                var ggg = w.name;                                    
                vvv.push(ggg);
            })
        })
    }
})

In console.log(vvv) I got the output as:

["22", "A", "23", "B", "24", "C", "25", "D", "26", "E", "27", "F", "28", "29", "30", "31", "10", "32", "11", "33", "12", "34", "13", "35", "14", "15", "16", "17", "18", "19", "1", "2", "3", "4", "5", "6", "7", "8", "9", "20", "21"].

But I want the output as:

["A", "B","C","D","E", "F","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"].

1
  • Sort the array maybe??? Commented Jan 24, 2018 at 6:27

3 Answers 3

1

I'm writing blind, but this should do the trick.

res.sort(function(a, b) {
  var AisNumber = a % 1 === 0;
  var BisNumber = b % 1 === 0;
  if (AisNumber && BisNumber) { 
    return Number(a) - Number(b);
  } else if (AisNumber && !BisNumber) {
    return 1;
  } else if (!AisNumber && BisNumber) {
    return -1;
  } else {
    if (a < b) {
      return -1;
    } else if (a > b) {
      return 1;
    } else {
      return 0;
    }
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

by using your suggestion iam getting the output as ["A", "C", "D", "E", "B", "F", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
@venkatesh Isn't that the output you wanted?
yes but iwant in order as ["A", "B", "C", "D", "E", "F", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
0

You could check for number and sort this values at the bottom and the rest to top.

return isNumber(a) - isNumber(b) || a - b || a > b || -(a < b);
//     ^^^^^^^^^^^^^^^^^^^^^^^^^                                check group
//                                  ^^^^^                       order numbers
//                                           ^^^^^^^^^^^^^^^^^  order strings

var array = ["22", "A", "23", "B", "24", "C", "25", "D", "26", "E", "27", "F", "28", "29", "30", "31", "10", "32", "11", "33", "12", "34", "13", "35", "14", "15", "16", "17", "18", "19", "1", "2", "3", "4", "5", "6", "7", "8", "9", "20", "21"];

array.sort(function (a, b) {
    function isNumber(v) { return (+v).toString() === v; }
    return isNumber(a) - isNumber(b) || a - b || a > b || -(a < b);
});

console.log(array)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

sort the array?

vvv.sort();

That works for me. But and you can sort the numbers as well, e.g:

var arr = ["A", "9", "B", "1", "8", "9", "C", "10", "11", "17", "20", "21", "30", "31", "35"];
for (var i = 0; i < arr.length; i++) {
    try{
        arr[i] = eval(arr[i]);
    }
    catch (err) {
        //ignore
    }
}
arr.sort();
alert(arr);

2 Comments

i have used the vvv.sort but the alphabets are coming in the middle of numbers
That's odd... for me it works as expected: var arr = ["A", "B", "C", "D", "E", "F", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]; arr.sort(); alert(arr);

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.