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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
mpora
  • 1,411
  • 5
  • 24
  • 65

4 Answers4

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>");
Community
  • 1
  • 1
Rudolf Mühlbauer
  • 2,511
  • 16
  • 18
  • 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: http://jsbin.com/epuvew/2/edit (give it about 8 seconds to execute). – Tom Smilack Oct 12 '12 at 19:17
  • Thanks for the benchmark! Interesting! But: rudolf: 3100, tom: 3371. (Chromium 18.0.1025.168) – Rudolf Mühlbauer Oct 12 '12 at 19:36
  • tom: 6618, rudolf: 4304 (Node.js 0.4.9) – Rudolf Mühlbauer Oct 12 '12 at 19:43
  • tom: 12642, rudolf: 15042 (Firefox 15). Really interesting! – Rudolf Mühlbauer Oct 12 '12 at 19:48
  • about readability, i think it depends whether you are used to functional programming or not. some people consider lisp as difficult to read ;) – Rudolf Mühlbauer Oct 12 '12 at 19:51
  • I am new to method chaining and I am always amazed at what can be accomplished in less lines of code. I want to constantly apply that method. – mpora Oct 12 '12 at 20:05
  • Interesting! I'm getting tom: 1907, rudolf: 3213 on Chrome 22.0.1229.94 m. I didn't realize how implementation-specific it would be. I'll also admit that the chained version isn't too bad, the readability comment was more about the block with `l1`, `l2`, etc. – Tom Smilack Oct 15 '12 at 16:24
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>

Mateus Schneiders
  • 4,853
  • 3
  • 20
  • 40
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.

Tom Smilack
  • 2,057
  • 15
  • 20
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>"));
Satyajit
  • 3,839
  • 1
  • 19
  • 14