O poder da programação funcional

Dado isso:

var s = "|L0011|0||BOOKED||To Be Paid Off|4453731.62|0|4||L0011|||L0011|MTG|44|CD|5.500%|09/01/16|04/28/06|44944.32|4650000|4453731.62|4650000<br>|L0013|0||BOOKED||Partial|102731.6|0|4||L0013|||L0013|MTG|44|CD|4.150%|11/01/15|01/15/07|2632.39|229004|102731.6|111768.94<br>|L0017|0||BOOKED||Other|11731845.23|0|4||L0017|||L0017|MTG|44|CD|6.025%|06/01/12|06/01/07|125463.07|14838606|11731845.23|14838606<br>|L0003|0||BOOKED|X|*|486636.93|0|4||L0003|||L0003|MTG|44|CD|5.450%|02/01/16|01/16/04|4440.97|1471875|486636.93|519009.7<br>|L0021|0||BOOKED||Cash Secured|6268274.18|0|4||L0021|||L0021|MTG|44|CD|0.750%|01/01/13|01/31/11|48689.83|6740078|6268274.18|6384189.98<br>";

Disto:

var sortThis = function(){   

//create an array containing column 2
for (i=0; i < rows.length; i++)
{
var col2 = rows[i].toString().split(colDelimiter)[1];

//create new array to hold column values
if(col2 !=undefined){
cols2
.push(col2);
}
}

//sort an array containing column 2
cols2
.sort();

//combine sorted array
for (q=0; q < cols2.length; q++){
for (i=0; i < rows.length; i++)
{
var col2 = rows[i].toString().split(colDelimiter)[1];

if (cols2[q] == col2)
{
SortedArray.push(rows[i]);
}
}
}

var sorted = SortedArray.join("<br />");
document
.getElementById("sorted").innerHTML = SortedArray;

};

Para isso:

var rowDel = "<br>";
var colDel = "|";

var Sort = function() {

var sorted = s.split(rowDel)
.map(function(row){return row.split(colDel); })
.sort(function(current,next){return current[1].localeCompare(next[1]); })
.map(function(col){return col.join(colDel); })
.join(rowDel);

document
.getElementById("sorted").innerHTML = sorted;

};