简体   繁体   English

用字符串和数字对Javascript数组进行排序

[英]Sorting Javascript array with strings and numbers

I have a list of elements in an array in Javascript as follows: 我在Javascript数组中有一个元素列表,如下所示:

myArray = ["Datastore one - free space 34.23GB", "Datastore two - free space 56.23GB",...]

and so on. 等等。 I would like to sort the array on the freespace, so in the example above Datastore two would be the first element in the array. 我想在自由空间上对数组进行排序,因此在上面的数据存储区示例中,两个将是数组中的第一个元素。 The array is always constructed with "- free space xx.xxGB", but the free space could be 5 digits in some cases, so xxx.xxGB for example. 数组始终以“-可用空间xx.xxGB”构造,但是在某些情况下可用空间可以是5位数字,例如xxx.xxGB。

Can anyone help provide a way of sorting the array please? 任何人都可以帮助提供一种对阵列进行排序的方法吗? I see I can use something like 我看我可以用类似的东西

"*- free space\s[1-9][0-9]*GB"

So would this be like 所以这就像

myArray.sort("*- free space\s[1-9][0-9]*GB") ?

Is this correct or how would I do this? 这是正确的还是我将如何做? Many thanks in advance. 提前谢谢了。

Pull the numeric parts out in a custom sort function, and subtract: 在自定义排序功能中拉出数字部分,然后减去:

 myArray = ["Datastore one - free space 34.23GB", "Datastore two - free space 56.23GB", "Datastore three - free space 6.23GB" ]; var re = /([0-9\\.]+)GB/; // regex to retrieve the GB values myArray = myArray.sort( function(a, b) { var ma = a.match(re); // grab GB value from each string var mb = b.match(re); // the result is an array, with the number at pos [1] return (ma[1] - mb[1]); } ); alert(myArray.join("\\r\\n")); 

This should do the trick: 这应该可以解决问题:

myArray.sort(function compare(a, b) {
  var size1 = parseFloat(a.replace(/[^0-9\.]/g, ''));
  var size2 = parseFloat(b.replace(/[^0-9\.]/g, ''));
  if (size1 < size2) {
    return -1;
  } else if (size1 > size2) {
    return 1;
  }
  return 0;
});

Array.prototype.sort does not accept a regex, it accepts a callback or will do its best to sort your array based on numeric/alphabetical order if you don't pass a callback Array.prototype.sort不接受正则表达式,接受回调,或者如果不传递回调,则将根据数字/字母顺序尽力对数组进行排序

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

This should work as well if you just want the numbers returned. 如果您只想返回数字,则此方法也应适用。

I split the string by spaces and grab the last section. 我将字符串按空格分开,然后抓住最后一部分。 (#GB), I then grab the substring of everything but the last two characters (so I chop off the GB), I then use the Javascript function to sort the remaining numbers. (#GB),然后抓取除最后两个字符以外的所有内容的子字符串(因此我砍掉了GB),然后使用Javascript函数对其余数字进行排序。

JSFiddle Demo JSFiddle演示

window.onload = function() {
   myArray = ["Datastore one - free space 34.23GB", "Datastore two - free space 56.23GB", "Datastore two - free space 16.23GB", "Datastore two - free space 6.23GB"];
    for (i = 0; i < myArray.length; i++) 
    { 
      var output  = myArray[i].split(" ").pop(); 
      output = output.substring(0, output.length-2);
      myArray[i] = output;
    }
   myArray.sort(function(a, b){return a-b});
   alert(myArray);
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM