0

I have intervals in an Array when I tried to use sort() to sort then it gives me wrong answer and could not able to sort it.. does someone have any idea how can I sort this. here is what I tried

array=["1050-3000","150-250","1-49","3001-9999","251-400","401-600","601-1049","50-149"]

When I sort it:- array.sort(); It gives me this answer:-

["1-49","1050-3000","150-250","3001-9999","251-400","401-600","601-1049","50-149"]

but what I expect is:-

["1-49","50-149","150-250","251-400","401-600","601-1049","1050-3000","3001-9999"]
5
  • 4
    Nope, your expectations are wrong ... Use the argument of .sort function to manipulate the order. Commented Feb 22, 2018 at 15:08
  • @Teemu how will I give the expected value then Commented Feb 22, 2018 at 15:10
  • 1
    Also check this: asciitable.com When sorting strings alphabetically, this is the order of characters. Actually '-' preceeds numbers. Commented Feb 22, 2018 at 15:23
  • Array.sort() works as designed. Your array contains strings, it sorts them by their dictionary order. Commented Feb 22, 2018 at 15:25
  • OK, Understand :) Commented Feb 22, 2018 at 15:26

5 Answers 5

7

You have to split the string and compare the first element.

let array = ["1050-3000", "150-250", "1-49", "3001-9999", "251-400", "401-600", "601-1049", "50-149"];

array.sort((a, b) => a.split("-")[0] - b.split("-")[0]);

console.log(array);

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

Comments

2

You need to split, convert to Number and then compare

var arr = ["1-49","50-149","150-250","251-400","401-600",,"601-1049","1050-3000","3001-9999"];
arr.sort( ( a, b ) => ( 
     al = +a.split("-")[1],  //last of a, after split by -
     bf = +b.split("-")[0],  //first of b, after split by -
     al-bf ) );

Comments

2

Replace dashes and sort. You don't need to split to get an array and then access the first position.

var array=["1050-3000","150-250","1-49","3001-9999","251-400","401-600","601-1049","50-149"];

array.sort((a, b) => a.replace('-', '') - b.replace('-', ''));

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

5 Comments

Can you please tell me what (a,b) does here and which value they took
@JustStartedProgramming a, b are the values within the array that will be compared, for example: a = "1050-3000" and b = "150-250", and so on.
and a.replace('-', '') - b.replace('-', '') here - is used to compair right?
@JustStartedProgramming basically to know is a > b, i.e: "10503000" > "150250" = true
I appreciate your explanation :)
1

The sorting function is giving you the right output because they are strings, not numbers. You will have to write custom logic to handle this.

Comments

0

You have to split the dashes so as to compare the values in the array when using the array.sort(); function

Here, try this:

var array = ["1050-3000", "150-250", "1-49", "3001-9999", "251-400", "401-600", "601-1049", "50-149"];

array.sort((a,b) => a.split("-")[0] - b.split("-")[0]);

console.log(array);

Comments

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.