0
DATA = ["Ge_total_EN394" , "p1@5end_chr2:191878874..191878938,-" , "p23@5end_chr2:191839657..191839662,-" , "p2@5end_chr2:191878812..191878823,-"]

I would like to sort the data in ascending order. The function I am using at the moment to sort is following

function sort(s1, s2) {
  var s1lower = s1.toLowerCase();
  var s2lower = s2.toLowerCase();
  return s1lower > s2lower? 1 : (s1lower < s2lower? -1 : 0);
}

however this gives me following output

"Ge_total_EN394" , "p1@5end_chr2:191878874..191878938,-" , "p23@5end_chr2:191839657..191839662,-" , "p2@5end_chr2:191878812..191878823,-"

The p23 is sitting before p2, which is not correct . I am not sure how to sort data properly so that p2 comes before

3
  • 2
    So why should p2@... string come before p23@...? What is the formal sorting rule for that? Commented Oct 21, 2016 at 1:49
  • 1
    p23 does indeed come lexically before p2@ - because 3 (charcode 51) is less than @ (charcode 64) Commented Oct 21, 2016 at 1:49
  • is there is a piece of code that can only sort alphanumeric characters and disregard any special characters Commented Oct 21, 2016 at 1:59

1 Answer 1

1

is there is a piece of code that can only sort alphanumeric characters and disregard any special characters

Yes, this is easy with a regex replace to remove all non-alphanumeric characters:

function sort(s1, s2) {
  var s1lower = s1.toLowerCase().replace(/[^a-z0-9]/g, "");
  var s2lower = s2.toLowerCase().replace(/[^a-z0-9]/g, "");
  return s1lower > s2lower? 1 : (s1lower < s2lower? -1 : 0);
}

However, I don't think simply disregarding "special" characters is what you actually need, because if you just keep the alphanumeric characters you'd have:

"p235endchr2191839657191839662"   // originally "p23@..."
"p25endchr2191878812191878823"    // originally "p2@..."

...and so the p23 item would still come before the p2 item.

What you seem to actually want is if there is an @ symbol then sort by the part before the @, so perhaps something like:

function sort(s1, s2) {
  var s1lower = s1.toLowerCase().split("@");
  var s2lower = s2.toLowerCase().split("@");
  if (s1lower[0] > s2lower[0])
    return 1;
  else if (s1lower[0] < s2lower[0])
    return -1;
  else
    return s1lower[1] > s2lower[1] ? 1 : s1lower[1] < s2lower[1] ? -1 : 0;
}

DATA = ["Ge_total_EN394" , "p1@5end_chr2:191878874..191878938,-" , "p23@5end_chr2:191839657..191839662,-" , "p2@5end_chr2:191878812..191878823,-", "p23@1end_chr2:191878812..191878823,-"];

DATA.sort(sort);
console.log(DATA);

(I've changed it to use an if/else if/else to make it more readable than it would be with several chained ternary operators.)

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

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.