Javascript:按频率对数组中的元素进行排序

时间:2020-10-08 11:26:14

标签: javascript arrays

有一个数组,其元素是按随机顺序排列的相同项目。我想对数组重新排序,以使高频的项目首先出现,然后低频的项目最后出现。

基本上,返回下面结果的代码是什么?

const a = [1,5,3,3,2,3,4,3,3,2,4,4,4];

const result = [3,3,3,3,3,4,4,4,4,2,2,1,5];

非常感谢!

5 个答案:

答案 0 :(得分:2)

计算频率,然后根据频率对数组进行排序:

let a = [1,5,3,3,2,3,4,3,3,2,4,4,4];

const freq = a.reduce((c, v) => (c[v] = (c[v] || 0) + 1, c), {});

a.sort((x, y) => freq[y]-freq[x] || y-x);

console.log(a);

答案 1 :(得分:1)

您需要收集相同的值,按长度排序并获得一个平面数组。

const
   array = [1,5,3,3,2,3,4,3,3,2,4,4,4],
   result = array
       .reduce((r, v) => {
           const temp = r.find(([w]) => v === w);
           if (temp) temp.push(v);
           else r.push([v]);
           return r;
       }, [])
       .sort((a, b) => b.length - a.length)
       .flat();

console.log(...result);

答案 2 :(得分:0)

您可以收集频率图,对其进行排序,然后按值和频率生成结果数组。

menu => array(
    main => array(
        1023 => array(
            id => 1023
            title => 'Menu item 1',
            children => null
        ),
        1024 => array(
            id => 1024
            title => 'Menu item 1',
            children => array(
                3000 => array(
                  id => 3000
                  title => 'Child menu item 1',
                  children => null
                ),
                3001 => array(
                  id => 3001
                  title => 'Child menu item 2',
                  children => null
                )
            )
        ),
        1024 => array(
            id => 1024
            title => 'Menu item 1',
            children => array(
                3000 => array(
                    id => 3000
                    title => 'Child menu item 1',
                    children => array(
                        4001 => array(
                          id => 4001
                          title => 'Child menu item 2',
                          children => null
                        ),
                        4002 => array(
                          id => 4002
                          title => 'Child menu item 2',
                          children => null
                        )
                    )
                ),
                3001 => array(
                  id => 3001
                  title => 'Child menu item 2',
                  children => null
                )
            )
        )
    )
)

答案 3 :(得分:0)

您还可以使用一个对象来存储每个数字出现多少次并在此基础上对数组进行排序。

 let a = [1, 2, 3, 3, 2, 3, 4, 3, 3, 2, 4, 4, 4];
    
    let obj = {};
    for (num of a) {
      if (obj[num] === undefined) {
        obj[num] = 1;
      } else {
        obj[num] += 1;
      }
    }
    
    Object.keys(obj)
      .sort((a, b) => obj[b] - obj[a])
      .map((key) => Array.from({ length: obj[key] }, () => parseInt(key)))
      .flat();

/*
OUTPUT: [3, 3, 3, 3, 3, 4, 4, 4, 4, 2, 2, 2, 1 ]
*/

答案 4 :(得分:0)

function compareValues(a, b) {
  return (a.localeCompare && b.localeCompare)
    ? a.localeCompare(b)
    : ((a < b && -1) || (a > b && 1) || 0);
}
function compareValueLists(a, b) {
  return (b.length - a.length) || compareValues(a[0], b[0]);
}

function collectEqualValue(collector, value) {
  const { index, list } = collector;
  let valueList = index[value];

  if (!valueList) {
    valueList = index[value] = [];

    list.push(valueList);
  }
  valueList.push(value);

  return collector;
}
const sampleList = [5, 1, 3, 3, 2, 3, 4, 3, 3, 2, 4, 4, 4];

console.log(
  ...sampleList
    .reduce(collectEqualValue, { index: {}, list: [] }).list
    .sort(compareValueLists)
    .flat()
);

相关问题