javascript:更改所有键的值顺序

Posted

技术标签:

【中文标题】javascript:更改所有键的值顺序【英文标题】:javascript: changing order of values for all keys 【发布时间】:2019-04-09 08:39:41 【问题描述】:

我有一个具有以下结构的对象:

object =  ids: [], labels:[], values:[] 

每个键有 80 个值。我想按降序(从大到小)对“values”键进行排序,并相应地更新 ids 和 labels 键(因此最高“values”值的标签和 id 现在排在第一位)。我尝试了以下代码,但它不起作用:

function getSorted(arr, sortArr) 
  var result = [];
  for(var i=0; i<arr.length; i++) 
    console.log(sortArr[i], arr[i]);
    result[i] = arr[sortArr[i]];
  
  return result;

我要排序的数据是这样的:

ids = [41, 121, 159, 165, 170, 189, 258, 259, 307, 340, 342, 352, 357, 412, 482, 513, 725, 830, 833, 874, 907, 944, 1167, 1169, 1189, 1193, 1208, 1232, 1274, 1314, 1497, 1498, 1503, 1505, 1795, 1950, 1959, 1960, 1962, 1968, 1977, 2011, 2024, 2039, 2065, 2077, 2110, 2167, 2178, 2184, 2186, 2188, 2191, 2235, 2244, 2247, 2264, 2275, 2291, 2318, 2335, 2342, 2350, 2396, 2419, 2475, 2483, 2491, 2546, 2571, 2722, 2737, 2739, 2782, 2811, 2859, 2908, 2936, 2964, 3450]

values = [71, 2, 2, 12, 2, 47, 2, 2, 7, 2, 2, 50, 2, 2, 113, 2, 2, 10, 3, 36, 3, 19, 163, 2, 51, 6, 5, 2, 4, 3, 2, 2, 2, 2, 10, 25, 30, 2, 3, 2, 40, 11, 13, 5, 2, 23, 2, 5, 7, 19, 3, 2, 28, 2, 14, 11, 78, 22, 2, 40, 3, 2, 2, 11, 13, 2, 2, 2, 2, 2, 8, 4, 4, 12, 13, 126, 7, 3, 10, 37]

labels = ["Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Actinomycetaceae", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Actinomycetaceae;Varibaculum", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Corynebacteriaceae", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Corynebacteriaceae;Corynebacterium", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Prevotellaceae", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Prevotellaceae;Prevotella", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes;Bacilli;Bacillales;Staphylococcaceae;Staphylococcus", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Finegoldia", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Gallicola", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Gallicola", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;Peptococcaceae;Peptococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;Ruminococcaceae", "Bacteria;Firmicutes;Clostridia;Clostridiales;Veillonellaceae", "Bacteria;Proteobacteria;Epsilonproteobacteria;Campylobacterales;Campylobacteraceae;Campylobacter"]

我应该如何解决这个问题?谢谢

【问题讨论】:

【参考方案1】:

三组数据之间唯一的关系是每个数组内部的索引,排序时会丢失。为了保持三组数据配对,我将映射您的数据并为每组创建一个对象:

const data =  ids, labels, values ;

const list = data.values.map((value, idx) => 
  return 
    id: data.ids[idx],
    value,
    label: data.labels[idx]
  ;
);

现在list 包含一个对象数组:

[
  
    id: 41,
    label: 'Bacteria',
    value: '71',
  ,
  
    // ... etc
  
]

这是一个更容易使用的数据结构,因为您希望 idlabelvalue 保持配对。

此时,您可以轻松地按 value 属性的降序对这个对象数组进行排序,如下所示:

list.sort((a, b) => 
  return b.value - a.value;
);

现在list 是一个数据对象数组,按object.value 降序排序。

您可以使用这个对象数组,或者轻松地将其转换回三个不同数组的原始格式。

【讨论】:

【参考方案2】:

我知道这与您的要求有些不同,但也许您可以了解如何适应您的需求。

之前我做了这个函数来解决我的对象的排序问题。

function sortObj(obj, index, sorting) 
  let ans = [];
  sorting = sorting.toUpperCase();

  if (obj.constructor === Array) 
    for (let i=0; i < obj.length; i++) 
      ans.push(obj[i]);
    
  
  else 
    for (let k in obj) 
      if (obj.hasOwnProperty(k)) 
        ans.push(obj[k])
      
    
  

  if (!ans.length)
    return [];


  if (isNaN(ans[0][index])) 
    ans = ans.sort(function (a,b) 
      if (a[index]) 
        if (sorting === "ASC")
          return a[index].localeCompare(b[index]);
        else
          return b[index].localeCompare(a[index]);
      
      return [];
    );
  
  else 
    ans = ans.sort(function (a, b) 
      if (sorting === "ASC")
        return a[index] - b[index];

      else
        return b[index] - a[index];
    )
  

  return ans;


// structure of object
obj = "1": id: "1", label: "A", value: "5", "2": id: "2", label: "B", value: "1"


sorted = sortObj(obj, "label", "ASC");
// [id: "1", label: "A", value: "5", id: "2", label: "B", value: "1"]


sorted = sortObj(obj, "value", "ASC");
// [id: "2", label: "B", value: "1", id: "1", label: "A", value: "5"]

【讨论】:

以上是关于javascript:更改所有键的值顺序的主要内容,如果未能解决你的问题,请参考以下文章

如何维护 Java 属性文件中键的顺序?

Java键盘密钥代码列表

根据首选顺序更改字典键的顺序

在 React Native Expo 中使用 react-redux 更改另一个键的值

SQLite。按特定顺序更新主键的值

更新/更改存储在 NSArray 中的 NSDictionary 键的值