如何仅对数组中的数字值排序?

时间:2019-02-14 11:19:27

标签: javascript arrays sorting

var points = [2,a,t,5,4,3]; 然后排序: var points = [2,a,t,3,4,5];

有什么帮助吗?因为我从事Adobe DC项目,所以可以使用旧式javascript代码发布您的帮助吗?

我的代码在那里:

var points = [t1, t2, t3, t4];
[t1, t2, t3, t4] = points;

var lucky = points.filter(function(number) {
  return isNaN(number) == false && number !=="" && number!== undefined;
});
points=lucky
points.sort(function(a, b){return a - b});

this.getField("Text6").value = points

但这只是最小到最大排序并过滤其他字符...我需要保留其他字符并且仅短数字...

4 个答案:

答案 0 :(得分:3)

var points = [2, "a", "t", 5, 4, 11, "", 3];
var insert = points.slice(0); // Clone points

// Remove all elements not a number
points = points.filter(function(element) {
  return typeof element === 'number';
});

// Sort the array with only numbers
points.sort(function(a, b) {
  return a - b;
});

// Re-insert all non-number elements
insert.forEach(function(element, index) {
  if (typeof element !== 'number') {
    points.splice(index, 0, element);
  }
});

console.log(points);

答案 1 :(得分:2)

如果您只是重新分配适当的值,则可以避免使用Expression<Func<Log, DateTime>>,从而可以更有效地执行此操作。

过滤,排序,重新分配以对数字进行排序:

CREATE FUNCTION dbo.CreateDateTime
(
    @year SMALLINT,
    @month SMALLINT,
    @day SMALLINT,
    @time FLOAT
)
RETURNS DATETIME AS
BEGIN
    DECLARE @paddedYear VARCHAR(4) = REPLACE(STR(@year, 4), SPACE(1), '0')
    DECLARE @paddedMonth VARCHAR(2) = REPLACE(STR(@month, 2), SPACE(1), '0')
    DECLARE @paddedDay VARCHAR(2) = REPLACE(STR(@day, 2), SPACE(1), '0')
    DECLARE @hours FLOAT = ROUND(@time / 10000, 0, 1)
    DECLARE @hoursAndMinutes FLOAT = ROUND(@time / 100, 0, 1)
    DECLARE @hoursMinutesAndSeconds FLOAT = ROUND(@time, 0, 1)
    --DATETIME only supports 3 decimal places on seconds so VARCHAR 5 is the max needed for the string 0.xxx
    DECLARE @milliseconds VARCHAR(5) = CAST(@time - ROUND(@time, 0, 1) AS VARCHAR(5))
    RETURN IIF
    (
        @year IS NULL
        OR @month IS NULL
        OR @day IS NULL,
        NULL,
        CAST
        (
            IIF
            (
                @time IS NULL,          
                @paddedYear
                + @paddedMonth
                + @paddedDay,
                @paddedYear
                + @paddedMonth
                + @paddedDay
                + ' '
                --In ROUND, the final argument of 1 forces ROUND to always round down (desired here).
                + REPLACE(STR(@hours, 2), SPACE(1), '0')
                + ':'
                + REPLACE(STR(@hoursAndMinutes - @hours * 100, 2), SPACE(1), '0')
                + ':'
                + REPLACE(STR(@hoursMinutesAndSeconds - @hoursAndMinutes * 100, 2), SPACE(1), '0')
                + IIF
                (
                    @time <> @hoursMinutesAndSeconds,
                    '.'
                    + SUBSTRING(@milliseconds, 3, LEN(@milliseconds) - 2),
                    ''
                )
            )
            AS DATETIME
        )
    )
END

答案 2 :(得分:1)

filter()数字超出数组。然后sort(),然后遍历真实数组。并检查value是否为Number,将其更改为已排序数组的第一个元素。并删除sortedArr的第一个元素

let arr = [5, 't', 's', 2,3,4];
let sortedNums = arr.filter(x => typeof x === 'number').sort((a,b) => a-b);
arr.forEach((a,i) => {
  if(typeof a === 'number'){
    arr[i] = sortedNums[0]
    sortedNums.shift();
  } 
})

console.log(arr)

答案 3 :(得分:0)

以下应根据您的输入给出确切的输出:

var result = [2, 'a', 't', 5, 4, 3].reduce(
  function(result,item){
    return !isNaN(item)&&item!==''
      ? typeof (result[result.length-1] && result[result.length-1].push) === 'function'
        ? (result[result.length-1].push(item),result)
        : result.concat([[item]])
      : result.concat(item)
  },
  []
).map(
  function(item){
    return typeof item.sort === 'function'
      ? item.sort(function(a,b){return a-b})
      : item
  }
).reduce(
  function(result,item){
    return result.concat(item)
  },[]
);

console.log(result);