用Lodash排序数字字符串数组 [英] sort array of numeric strings with Lodash

查看:566
本文介绍了用Lodash排序数字字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_.sortBy(arrData, "rhid");

此代码对数组进行排序,但由于字段"rhid"的值为字符串,因此顺序混乱. 我该如何排序,好像"rhid"所在的int字段一样.

This code sorts array but as the values of field "rhid" are strings the order is messed up. How can i sort as if "rhid" where int field.

谢谢

推荐答案

sortBy可以用于函数而不是属性名称.

sortBy can be used with a function instead of a property name.

_.sortBy(arrData, function (obj) {
    return parseInt(obj.rhid, 10);
});

这篇关于用Lodash排序数字字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆