按关键对象

时间:2015-04-24 09:46:46

标签: javascript jquery arrays sorting

我有以下结构:

var participant0 = {
    name : "",
    nickname : "",
    number "99999"
} ;

var participant1 = {
    name : "bbb",
    nickname : "",
} ;

var participant2 = {
    name : "",
    nickname : "aaa"
} ;

var participant3 = {
    name : "ccc",
    nickname : ""
} ;

我有一个包含结构实例的数组:

var array = [participant0, participant3, participant1, participant2];

我想按字母顺序排列这个数组。名字第一,昵称第二名。如果这两个键不存在,我想检查数字键并将此元素放在排序列表的末尾。

预期结果:

var array = [participant2, participant1, participant3, participant0];

(要按&#34排序对象; aaa"," bbb"," ccc"," 9999")

以下代码可以正常工作以按名称或昵称排序,但我不知道如果有数字键,如何将项目放在排序列表的末尾:

fav_list.sort(function(x, y) {
              return (x.participant.name || x.participant.nickname).localeCompare(y.participant.name || y.participant.nickname);
            });

5 个答案:

答案 0 :(得分:2)

我认为应该这样做,请参阅代码中的注释:

fav_list.sort(function(x, y) {
    var xvalue = x.participant.name || x.participant.nickname;
    var yvalue = y.participant.name || y.participant.nickname;
    // By name if both have names
    if (xvalue && yvalue) {
        return xvalue.localeCompare(yvalue);
    }
    // Names always come before numbers
    if (xvalue) {
        return -1;
    }
    if (yvalue) {
        return 1
    }
    // Otherwise compare numbers
    return (x.participant.number || 0) - (y.participant.number || 0);
});

您可能希望在最后一行使用默认数字(零)。

请注意,您显示的数组与该代码不完全匹配,因为数组直接使用参与者,但代码期望参与者位于对象的participant属性上。

带有轻微调整数组的实例,以及一种方便的方式来说明我们最终的订单:

var participant0 = {
  name: "",
  nickname: "",
  number: "99999",
  debug: 0
};

var participant1 = {
  name: "bbb",
  nickname: "",
  debug: 1
};

var participant2 = {
  name: "",
  nickname: "aaa",
  debug: 2
};

var participant3 = {
  name: "ccc",
  nickname: "",
  debug: 3
};

var array = [
  {
    participant: participant0
  },
  {
    participant: participant3
  },
  {
    participant: participant1
  },
  {
    participant: participant2
  }
];

array.sort(function(x, y) {
  var xvalue = x.participant.name || x.participant.nickname;
  var yvalue = y.participant.name || y.participant.nickname;
  // By name if both have names
  if (xvalue && yvalue) {
    return xvalue.localeCompare(yvalue);
  }
  // Names always come before numbers
  if (xvalue) {
    return -1;
  }
  if (yvalue) {
    return 1
  }
  // Otherwise compare numbers
  return (x.participant.number || 0) - (y.participant.number || 0);
});
array.forEach(function(entry) {
  snippet.log(entry.participant.debug);
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

答案 1 :(得分:1)

不要一次做所有事情。

fav_list.sort(function(x,y) {
    var xname = x.name || x.nickname || "\uffff"+x.number;
    var yname = y.name || y.nickname || "\uffff"+y.number;
    return xname.localeCompare(yname);
});

"\uffff"是强制数字到最后;)

答案 2 :(得分:0)

代码可以写成:

  

\ uffff 直接被解释为所需的   字符串,整个序列实现为单个字符

&#13;
&#13;
var participant0 = {
    name : "",
    nickname : "",
    number: "99999"
} ;

var participant1 = {
    name : "bbb",
    nickname : ""
} ;

var participant2 = {
    name : "",
    nickname : "aaa"
} ;

var participant3 = {
    name : "ccc",
    nickname : ""
} ;
var array = [participant0, participant3, participant1, participant2];
console.log(array.sort(function(x,y) {
    var val1 = x.name || x.nickname || "\uffff"+x.number;
    var val2 = y.name || y.nickname || "\uffff"+y.number;
    return xname.localeCompare(val2);
}))
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

document.getElementById('demo').innerHTML ='<li>' + sensorNames + '</li>'

答案 4 :(得分:-1)

你可以尝试解决问题。

  var numbers = [];    
fav_list.sort(function(x, y) {  
             if(!isNaN(x)){
              numbers.push(x);
             continue;
             }
                  return (x.participant.name || x.participant.nickname).localeCompare(y.participant.name || y.participant.nickname);
                });
    fav_list.push(numbers);