3

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.

1
  • 2
    The order is space comes before A. Commented Aug 20, 2013 at 3:56

5 Answers 5

5

That's because you're not stripping off the surrounding spaces, especially the leading spaces. The sorting is off because a space comes before any letter. The below code should fix it, assuming the whole line has no surrounding white space:

// split on pipe and surrounding white space
var splitRe = /\s*\|\s*/;

carriersOne = carriersOne[0].split(splitRe);
carriersTwo = carriersTwo[0].split(splitRe);
Sign up to request clarification or add additional context in comments.

Comments

1

You need to trim the spaces:

carriersOne = carriersOne[0].split('|').map(function(e){return e.replace(/^\s*/, '')});
carriersTwo = carriersTwo[0].split('|').map(function(e){return e.replace(/^\s*/, '')});

1 Comment

just for sake of it.. i would have trimmed the whitespaces from the end too.. like this carriersOne[0].split("|").map(function(e){return e.replace(/^\s*/, '').replace(/\s*$/, '')});. I am sure, if there is a better way of doing it. @xdazz
0

ASCII of space is 32 and capital characters start from 65, and small letters from 97. This is why space is sorted before A and a.

You might want to trim this using regular expressions or simply taking advantage of underscore.js

Comments

0

The problem is that you have leading spaces in the carrier names.

     ["    Some Other Company", " Chicago Insurance Company ", " Hospital Insurance    Corporation", " New York Health Care Insurance Company ", "Advantage Care ", "St. Joseph's Medical Center "]

You can remove the leading spaces and get the result you're looking for like this:

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 = allCarriers.map(
                 function(carrier){ 
                       // replace leading and trailing spaces
                       return carrier.replace(/^\s+|\s+$/g,'');
                 }
               );
allCarriers.sort();
console.log(allCarriers);

Comments

0

It's the spaces in between the pipes. Here's a much fancier (and more flexible) solution.

http://jsfiddle.net/THEtheChad/3v7ss/

var carriersOne = ['St. Joseph\'s Medical Center | New York Health Care Insurance Company |    Some Other Company'];
var carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital Insurance    Corporation'];

var allCarriers = parseAndMerge(carriersOne, carriersTwo).sort();

var count = allCarriers.length;

allCarriers.forEach(function(carrier){
    alert(carrier);
});

function parseAndMerge(){
    var args = Array.prototype.slice.call(arguments)
      , collector = []
    ;//var

    args.forEach(function(arr){
        collector = collector.concat(arr[0].split('|').map(trim));
    });

    return collector;
}

function trim(string) { return string.trim() }

The spaces in between the pipes | are causing your sort order to be incorrect. Try something like this:

http://jsfiddle.net/THEtheChad/LQxW8/

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 = normalize(carriersOne);
carriersTwo = normalize(carriersTwo);

allCarriers = carriersOne.concat(carriersTwo);
allCarriers.sort();

count = allCarriers.length;

for(i=0;i<count;i++) {
   alert(allCarriers[i]);
}

function normalize(arr){
  var carriers = arr[0].split('|')
    , i = carriers.length
  ;//var

    while(i--){
      carriers[i] = carriers[i].trim();  
    }

   return carriers;
}

Comments

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.