带有字符串和字符串编号的JS排序数组

时间:2018-08-28 21:45:15

标签: javascript sorting

我正在尝试使用自定义值对数组进行排序,因为它是整数值。 有我的数组:

[ 'a>46', 'a>86', 'h>78' ]

和所需的输出:

[ 'a>46', 'h>78', 'a>86' ]

注意:在我的数组中可以找到的最大值为90。

我正在尝试这种方式:

  var newarr = [];
  var max = 91;
          for (let u = 0; u < array.length; u++) {
            var nmbr = parseInt(array[u].replace(/\D/g,'')); // get integer of array element

            if (nmbr < max) {       // if element is lower than current highest value
              max = nmbr;
              newarr[0] = array[u];  // assign it to the beggining of new array
            } else {   // else put it at as the next newarr element

              newarr[newarr.lenght+1] = array[u];
            }

          }

输出:

[ 'a>46', <1 empty item>, 'a>86' ]

3 个答案:

答案 0 :(得分:2)

我建议使用Array.prototype.sort()

var array = ["a>46", "h>78", "a>86"]
array.sort(function(a, b) {
  var number_a = parseInt(a.replace(/\D/g,''));
  var number_b = parseInt(b.replace(/\D/g,''));
  return number_a - number_b;
});

输出["a>46", "h>78", "a>86"]


如果您想进一步了解其实际工作原理,建议您在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort上查看compare函数示例

基本上:如果返回零,则ab相等。如果返回的负数表示a小于b。如果您返回的正数表示a大于b

答案 1 :(得分:1)

要获得预期的结果,请使用以下使用数组排序功能的选项

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:key name="CustInvoiceTrans" match="CustInvoiceTrans" use="ItemId"/>
  <xsl:key name="RefTaxTrans" match="TaxTrans" use="InventTransId"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="CustInvoiceJour">
      <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:variable name="group-heads"
             select="CustInvoiceTrans[generate-id() = generate-id(key('CustInvoiceTrans', ItemId)[1])]"/>
          <xsl:apply-templates select="$group-heads"/>
          <xsl:apply-templates select="$group-heads" mode="tax"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="CustInvoiceTrans/LineAmount">
      <xsl:copy>
          <xsl:value-of select="sum(key('CustInvoiceTrans', ../ItemId)/LineAmount)"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="CustInvoiceTrans" mode="tax">
      <TaxTrans class="entity">
          <xsl:variable name="referenced-tax" select="key('RefTaxTrans', key('CustInvoiceTrans', ItemId)/InventTransId)"/>
          <xsl:copy-of select="$referenced-tax[1]/InventTransId"/>
          <TaxAmount>
              <xsl:value-of select="sum($referenced-tax/TaxAmount)"/>
          </TaxAmount>
          <TaxBaseAmount>
              <xsl:value-of select="sum($referenced-tax/TaxBaseAmount)"/>
          </TaxBaseAmount>
          <xsl:copy-of select="$referenced-tax[1]/TaxValue"/>
      </TaxTrans>
  </xsl:template>

</xsl:stylesheet>

https://codepen.io/nagasai/pen/eLdYKz?editors=1010

答案 2 :(得分:0)

JavaScript带有一个内置的方法来对数组进行排序。

const array = [ 'a>46', 'a>86', 'h>78' ];

array.sort((a, b) => {
  a = Number.parseInt(a.replace(/\D/g, ''), 10);
  b = Number.parseInt(b.replace(/\D/g, ''), 10);
  return a - b;
});

console.log(array);

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/Number/parseInt