简体   繁体   English

如何按数字顺序对字符串数组进行排序?

[英]How to sort an array of strings by numerical order?

For example, i have a string array ['item 2','item 1','item 10','item 4'] .例如,我有一个字符串数组['item 2','item 1','item 10','item 4'] I want it to be like this ['item 1','item 2','item 4','item 10'] but by default, the sort() function sorts values by alphabetical order, which means it will look like this ['item 1','item 10','item 2','item 4']我希望它像这样['item 1','item 2','item 4','item 10']但默认情况下, sort() function 按字母顺序对值进行排序,这意味着它看起来像这样['item 1','item 10','item 2','item 4']

Just get the number and sort it只需获取数字并对其进行排序

 let array = ["item 1", "item 10", "item 2", "item 4"]; const result = array.sort((a, b) => a.match(/\d+/) - b.match(/\d+/)); console.log(result);

You can add custom compare function in array.sort您可以在 array.sort 中添加自定义比较function

 const myArr = ['item 2','item 1','item 10','item 4']; const sortArr = myArr.sort((a, b) => { const num1 = Number(a.split(' ')[1]); const num2 = Number(b.split(' ')[1]); return num1 - num2; }); console.log(sortArr);

or simply或者干脆

 const myArr = ['item 2','item 1','item 10','item 4']; const sortArr = myArr.sort((a, b) => Number(a.split(' ')[1]) - Number(b.split(' ')[1])) console.log(sortArr);

You can pass a function to sort() method and sort it on custom way您可以将 function 传递给sort()方法并以自定义方式对其进行排序

let result = arr.sort((a,b) => a.split(' ')[1]-b.split(' ')[1])

console.log(result);
let array = ['item 1','item 10','item 2','item 4']; var customSort = function (a, b) { return Number(a.replace("item ","")) - Number(b.replace("item ","")); } console.log(array.sort(customSort)); //one line console.log(array.sort((a,b) => Number(a.replace("item ","")) - Number(b.replace("item ",""))));

Make a custom sort() function and use slice to get the number of the string and then apply Number() to transform it to a number for comparison.制作自定义sort() function并使用slice获取字符串的编号,然后应用Number()将其转换为数字进行比较。

Note: sort() will update the original array.注意: sort()将更新原始数组。

 const array = ["item 1", "item 10", "item 2", "item 4"]; array.sort((a, b) => Number(a.slice(5)) - Number(b.slice(5))); console.log(array);

You can pick any answer.你可以选择任何答案。 This is a more generic solution, for sorting strings ending with numeric values (or not).这是一个更通用的解决方案,用于对以数值结尾(或不是)的字符串进行排序。

 const toNumberOrZero = str => { const maybeNumber = str.match(/(\d+)$/); return maybeNumber && +maybeNumber[0] || 0; }; const sortNumericForStringsEndingWithNumbers = (a, b) => toNumberOrZero(a) - toNumberOrZero(b); console.log( ['item 2', 'item 1', 'item 10', 'item 4', 'item222', 'anything1023', 'notanumber', 'the meaning of life is 42'].sort(sortNumericForStringsEndingWithNumbers) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按特定顺序对 JS 数值数组进行排序 - Sort a JS numerical array in specific order 按数字顺序合并和排序数字数组-JavaScript - Merge and sort an array of numbers in numerical order - javascript 如何在Jquery中以给定的任何特定排序顺序对字符串数组进行排序 - How to sort an array of Strings in Jquery with any specific sort order given 在 JS 中,如何根据我创建的指定顺序(不是字母或数字)对 object 数组进行排序? - In JS, how to sort over an object array on a value that's based on an assigned order I've created, not alphabetical or numerical? 基于整数而不是字符串按升序对数组进行排序 - Sort array in ascending order based on ints and not strings 排序一个字符串数组,使用另一个字符串数组确定顺序 - sort an array of strings, using another array of strings to determine the order 在字符串数组中,如何在字符串的一部分上对数组进行排序 - In an array of strings how to sort the array on part of the strings 如何按字符串中的数字对数组进行排序? - How to sort an array by numbers in strings? 如何在Javascript中以预先决定的顺序对字符串进行排序? - How to sort strings in a pre decided order in Javascript? 如何根据对象属性的数值对对象数组进行排序? - How to sort an array of objects based on the numerical value of an object property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM