【发布时间】:2017-02-27 07:50:01
【问题描述】:
我有以下对象数组,我想对其进行排序。
var points = [
{
val: '0.10',
text: '$0.10'
},
{
val: '0.50',
text: '$0.50'
},
{
val: '1',
text: '$1'
},
{
val: '2',
text: '$2'
},
{
val: '3',
text: '$3'
},
{
val: '10',
text: '$10'
},
{
val: '20',
text: '$20'
},
{
val: '25',
text: '$25'
},
{
val: '50',
text: '$50'
},
{
val: '75',
text: '$75'
},
{
val: '100',
text: '$100'
},
{
val: '150',
text: '$150'
},
{
val: '200',
text: '$200'
},
{
val: '250',
text: '$250'
},
{
val: '300',
text: '$300'
},
{
val: '400',
text: '$400'
},
{
val: '1000',
text: '$1,000'
},
{
val: 'Other',
text: 'Other'
}
]
我试过下面的排序功能-
function myFunction() {
points.sort(function(a, b){return a.val - b.val});
console.log(points)
}
但这会返回错误的输出。 我也尝试过使用 orderby 和 sortby 但仍然得到错误的输出。
_.orderBy(points,['val'],['asc'])
_.sortBy(points, 'val')
通过使用这两个选项,我得到了错误的排序值。
【问题讨论】:
-
numbers和strings的排序不相等。您在val字段中有strings。 -
我只是想做同样的事情@Rory McCrossan
标签: javascript jquery underscore.js lodash