3

这就是将数据提供给我的方式,在一个带有由管道字符分隔的元素的对象中。

首先我需要组合这两个数组,然后按字母顺序排序。下面的例子是一个简化的例子。但是有些东西正在关闭排序功能。结果很奇怪

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]);
}

你得到的是:

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

wt-bleep 命令是什么?注意:如果您使用单个名称,或可预测的名字和姓氏,它可以很好地组合和排序。

4

5 回答 5

5

那是因为您没有剥离周围的空间,尤其是前导空间。排序已关闭,因为在任何字母之前都有一个空格。下面的代码应该修复它,假设整行没有周围的空白:

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

carriersOne = carriersOne[0].split(splitRe);
carriersTwo = carriersTwo[0].split(splitRe);
于 2013-08-20T03:55:24.080 回答
1

您需要修剪空格:

carriersOne = carriersOne[0].split('|').map(function(e){return e.replace(/^\s*/, '')});
carriersTwo = carriersTwo[0].split('|').map(function(e){return e.replace(/^\s*/, '')});
于 2013-08-20T03:56:23.333 回答
0

问题是运营商名称中有前导空格。

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

您可以删除前导空格并获得您正在寻找的结果,如下所示:

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);
于 2013-08-20T03:59:05.270 回答
0

空格的 ASCII 是 32,大写字符从 65 开始,小写字母从 97 开始。这就是为什么空格排在 A 和 a 之前的原因。

您可能想使用正则表达式或简单地利用underscore.js来修剪它

于 2013-08-20T04:02:18.167 回答
0

这是管道之间的空间。这是一个更漂亮(也更灵活)的解决方案。

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() }

管道之间的空格|导致您的排序顺序不正确。尝试这样的事情:

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;
}
于 2013-08-20T03:58:45.703 回答