2

I have a string like this:

var allString = "|AQW12|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW09|2|34|33|12|<br>";

Basically this is some sort of table where column names would be delimetered by | and <br> delimits the rows.

My question is how you would go to sort the allString by column 2 (i.e. AQW12, AQW11, etc),.

Thank you

4 Answers 4

1

quick solution, excuse the bad variable names:

var allString = "|AQW12|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW09|2|34|33|12|<br>";

var l1 = allString.split("<br>");
var l2 = l1.map( function(elem) { return elem.split("|"); } )
var l3 = l2.sort( function(a,b) { return a[1].localeCompare(b[1]); } )
var l4 = l3.map( function(elem) { return elem.join("|"); } );
var output = l4.join("<br>");
console.log(allString);
console.log(output);

output:

|AQW12|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW09|2|34|33|12|<br>
|AQW09|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW12|2|34|33|12|<br>

the variable l3 contains the data in array form:

[ [ 'AQW09', '2', '34', '33', '12' ],
  [ 'AQW11', '2', '34', '33', '12' ],
  [ 'AQW12', '2', '34', '33', '12' ] ]

for comments on localCompare, refer to https://stackoverflow.com/a/2167619/1689451

of course, you could also use method chaining, if you like to show off :)

var output = allString.split("<br>")
  .map( function(elem) { return elem.split("|"); } )
  .sort( function(a,b) { return a[1].localeCompare(b[1]); } )
  .map( function(elem) { return elem.join("|"); } )
  .join("<br>");
Sign up to request clarification or add additional context in comments.

7 Comments

I'm sorry but I have to disagree. I find that very hard to read, and it's much slower than the more straightforward version: jsbin.com/epuvew/2/edit (give it about 8 seconds to execute).
Thanks for the benchmark! Interesting! But: rudolf: 3100, tom: 3371. (Chromium 18.0.1025.168)
tom: 6618, rudolf: 4304 (Node.js 0.4.9)
tom: 12642, rudolf: 15042 (Firefox 15). Really interesting!
about readability, i think it depends whether you are used to functional programming or not. some people consider lisp as difficult to read ;)
|
0
var allString = "|AQW12|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW09|2|34|33|12|<br>";

​var arr = allString.split("<br>");

var re=/AQW([\d]+)/;
​arr.sort(function(a,b){
    if(!(a && b)) return -1;
    x=a.split("|")[1];
    y=b.split("|")[1];

    return(parseInt(x.match(re)[1]) - parseInt(y.match(re)[1]));
});

console.log(arr.join("<br>"));

Comments

0

If this column is always in the same pattern, like 3 letters and 2 numbers (AQW12, AQW11).

You can do something like this:

var allString  = "|AQW12|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW09|2|34|33|12|<br>"
var allRows    = allString.split('<br>');
var sortedRows = allRows.sort(function(a,b){ 

  if(a && b ){
      var arrA = a.split('|');
      var arrB = b.split('|');

      return new Number(arrA[1].substr(3,2)) - new Number(arrB[1].substr(3,2));
  }

  return null;
});

var sortedString = sortedRows.join('<br>');

>> |AQW09|2|34|33|12|<br>|AQW11|2|34|33|12|<br>|AQW12|2|34|33|12|<br>

Comments

0

Probably the easiest way would be to split it into an array, sort it, and recombine it:

var rows = allString.split("<br>");
//sort...
var sortedString = rows.join("<br>");

JavaScript arrays have a built-in sort method that accepts a custom comparison function, which you could define to compare the second item in each string:

function compare(rowA, rowB) {

    var rowAcol2 = rowA.split("|")[1];
    var rowBcol2 = rowB.split("|")[1];

    return rowAcol2.localeCompare(rowBcol2);
}

And then pass into the sort method:

rows.sort(compare);

This JSBin shows it all together.

1 Comment

I implemented this idea and it worked before I came back here. Thank you.

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.