Monday, 19 August 2013

Combining then sorting Javascript arrays. Getting unpredictable results

Combining then sorting Javascript arrays. Getting unpredictable results

this is how the data has been given to me, in an object w/ elements
separated by the pipe char.
first i need to combine the two array and then sort alphabetically. the
below example is a simplified example. but something is throwing the sort
function off. the results are bizarre
carriersOne = ['St. Joseph\'s Medical Center | New York Health Care
Insurance Company | Some Other Company'];
carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital
Insurance Corporation'];
carriersOne = carriersOne[0].split('|');
carriersTwo = carriersTwo[0].split('|');
allCarriers = carriersOne.concat(carriersTwo);
allCarriers.sort();
count = allCarriers.length;
for(i=0;i<count;i++) {
alert(allCarriers[i]);
}
What you get is: Chicago Insurance Company Hospital Insurance Corporation
New York Health Care Insurance Company Some Other Company Advantage Care
St. Joseph's Medical Center
w-t-bleep order is that? note: if you use single names, or predictable
first and last names, it combines and sorts fine.

No comments:

Post a Comment