sort array of numeric strings with Lodash

感情迁移 提交于 2020-07-18 21:43:13

问题


_.sortBy(arrData, "rhid");

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.

Thanks


回答1:


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

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


来源:https://stackoverflow.com/questions/34724034/sort-array-of-numeric-strings-with-lodash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!