简体   繁体   English

如何按数字字符串和特殊字符对 object 的数组进行排序

[英]How to sort array of object by string of numbers and special characters

I have an array of some objects and I want to sort them by dimension property, where that property is a string of some number intervals and special characters.我有一些对象的数组,我想按dimension属性对它们进行排序,其中该属性是一些数字间隔和特殊字符的字符串。 Here is the example of what I have这是我所拥有的示例

 let data = [ { "soldTickets": 206, "soldRevenue": 12825.309997558594, "playedOffTickets": 2915, "playedOffRevenue": 495923.22019958496, "dimension": "10 - 25", "ticketsChange": -2709, "revenueChange": -483097.91020202637 }, { "soldTickets": 172, "soldRevenue": 17174.29998779297, "playedOffTickets": 2485, "playedOffRevenue": 467017.27951049805, "dimension": "25 - 50", "ticketsChange": -2313, "revenueChange": -449842.9795227051 }, { "soldTickets": 122, "soldRevenue": 9892.249984741211, "playedOffTickets": 9121, "playedOffRevenue": 1129196.3203125, "dimension": "> 200", "ticketsChange": -8999, "revenueChange": -1119304.0703277588 }, { "soldTickets": 52, "soldRevenue": 3159.239990234375, "playedOffTickets": 544, "playedOffRevenue": 88893.0400390625, "dimension": "0 - 10", "ticketsChange": -492, "revenueChange": -85733.80004882812 }, { "soldTickets": 38, "soldRevenue": 3162.1099700927734, "playedOffTickets": 476, "playedOffRevenue": 92432.79023742676, "dimension": "100 - 200", "ticketsChange": -438, "revenueChange": -89270.68026733398 }, { "soldTickets": 37, "soldRevenue": 3233, "playedOffTickets": 590, "playedOffRevenue": 97645.46026611328, "dimension": "50 - 100", "ticketsChange": -553, "revenueChange": -94412.46026611328 } ]; const toNumber = (str) => { if (Number(str)) { return Number(str); } else { return Number(str.substring(0, str.length - 1)); } }; data = data.sort( (a, b) => toNumber(String(a["dimension"]).split(' ')[0]) - toNumber(String(b["dimension"]).split(' ')[0]) ); console.log(data)

The problem is that the array is not sorted the way I wanted, since the > 200 should be the last in this case instead of being the first.问题是数组没有按照我想要的方式排序,因为> 200在这种情况下应该是最后一个而不是第一个。 The desired order should be like this所需的顺序应该是这样的

['0 - 10', '10 - 25', '25 - 50', '50 - 100', '100 - 200', '> 200']

What am I doing wrong?我究竟做错了什么? Thanks in advance!提前致谢!

If we can assume the first number is what we want to sort on, you can use a little regex to find the first number, and then we can sort on that.如果我们可以假设第一个数字是我们想要排序的,您可以使用一些正则表达式来找到第一个数字,然后我们可以对其进行排序。

Of course if your condition gets more complicated, like < 20 etc, then you would need to re-evaluate how you decide the ordering.当然,如果您的情况变得更复杂,例如< 20等,那么您需要重新评估您如何决定订购。

eg.例如。

 let data=[{soldTickets:206,soldRevenue:12825.309997558594,playedOffTickets:2915,playedOffRevenue:495923.22019958496,dimension:"10 - 25",ticketsChange:-2709,revenueChange:-483097.91020202637},{soldTickets:172,soldRevenue:17174.29998779297,playedOffTickets:2485,playedOffRevenue:467017.27951049805,dimension:"25 - 50",ticketsChange:-2313,revenueChange:-449842.9795227051},{soldTickets:122,soldRevenue:9892.249984741211,playedOffTickets:9121,playedOffRevenue:1129196.3203125,dimension:"> 200",ticketsChange:-8999,revenueChange:-1119304.0703277588},{soldTickets:52,soldRevenue:3159.239990234375,playedOffTickets:544,playedOffRevenue:88893.0400390625,dimension:"0 - 10",ticketsChange:-492,revenueChange:-85733.80004882812},{soldTickets:38,soldRevenue:3162.1099700927734,playedOffTickets:476,playedOffRevenue:92432.79023742676,dimension:"100 - 200",ticketsChange:-438,revenueChange:-89270.68026733398},{soldTickets:37,soldRevenue:3233,playedOffTickets:590,playedOffRevenue:97645.46026611328,dimension:"50 - 100",ticketsChange:-553,revenueChange:-94412.46026611328}]; data = data.sort( (a, b) => (a.dimension.match(/\d+/)[0] | 0) - (b.dimension.match(/\d+/)[0] | 0) ); console.log(data)

ps.附言。 the val | 0 val | 0 val | 0 , is just a quick way to convert a string into a number, similar to Number(val) , parseInt(val, 10) , if we don't convert to number, you would have 200 been less than 30 etc. val | 0 ,只是将字符串转换为数字的一种快速方法,类似于Number(val)parseInt(val, 10) ,如果我们不转换为数字,你会得到200小于30等等。

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

相关问题 如何对包含特殊字符和空格的字符串数组进行排序 - how to sort an array of string which contains special characters and white spaces 排序-将数字和特殊字符移到末尾 - Sort - Move numbers and special characters to the end 如何将字符串限制为不允许数字或特殊字符? - How do I restrict a string to not allow numbers or special characters? 如何排序数组但在字符串中查找数字之前? - How sort array but before find numbers in string? 如何根据字符串中的数字对数组进行排序? - How to sort array based on the numbers in string? 将具有特殊字符的字符串拆分为数组 - Split String with special characters to array 按字母顺序对字符串数组进行排序,然后按特殊字符对数字进行排序 - Sort an array of strings alphabetically, numerically then by special characters 当属性是字符串时,如何从左到右按属性对对象数组进行排序,首先是数字? - How to sort array of object by properties from left to right having numbers first when property is string? 如何将带有特殊字符的字符串修改为 javascript 中的对象数组 - how to modify string with special characters to array of objects in javascript 如何在字符串中查找特殊字符并将其存储在JavaScript中的数组中 - How to find special characters in a string and store in an array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM