0

I'm using double commas (,,) as my array split delimiters. So, when I go to split my array, I use:

    var flt = localStorage.getItem('flt');
    var fltArray = new Array();
    var fltArray = flt.split(",,");

When I sort the array, my sort code strips out the comma at the end of each element. So, the split between all the array elements becomes "," when it was originally ,","

I'm using the following code to sort my array.

fltOfferSellEconArray.sort(function(x,y){
         var xp = x.substr(0,4);
         var yp = y.substr(0,4);
         return xp == yp ? 0 : xp > yp ? -1 : 1;
   });

Is there a way to add a comma to the end of each element without having to loop through the array again?

Here is my array before sorting.....

["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0400199X13922155681178VplSv6axJ," ,"0375199X1392215732050ySP3sBJLTe,"]

This is what I NEED it to look like after sort. (note... it just moves the element starting with "0400" to the last element.....

["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0375199X1392215732050ySP3sBJLTe,","0400199X13922155681178VplSv6axJ,"]
3
  • 3
    Your question is unclear. The serialization-delimiter you use doesn't matter for sorting. Commented Feb 12, 2014 at 16:52
  • 2
    Can you give an example of what you original array looks like and what it's supposed to look like after sorting? Commented Feb 12, 2014 at 16:52
  • OK. I edited question to show what I need it to look like after sort. Sorry. Commented Feb 12, 2014 at 17:00

1 Answer 1

1

It seems to work fine for me:

> a = ["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0400199X13922155681178VplSv6axJ," ,"0375199X1392215732050ySP3sBJLTe,"]
> a.sort(function(x,y){
     var xp = x.substr(0,4);
     var yp = y.substr(0,4);
     return xp == yp ? 0 : xp > yp ? -1 : 1;
  });
> a
["0400199X13922155681178VplSv6axJ,", "0375199X1392215732050ySP3sBJLTe,", "0303199X13922153062748VplSv6axJ,", "0302199X1392215252490ySP3sBJLTe,", "0301199X1392215191700ySP3sBJLTe,", "0300199X1392215130873ySP3sBJLTe,", "0000999X12623220000009999999999,"]

What exactly doesn't work for you?

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

1 Comment

Thank you to everyone. My array before the sort was defective. Fixed that and everything working fine. Thanks @Claudiu for point this out!

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.