如何按字母顺序对单个对象进行排序?

4
我想按字母顺序对单个对象进行排序...保留其键和值。例如: 输入为: {2: "a", 3: "e", 4: "h", 5: "g", 6: "d", 7: "i", 8: "c", 9: "f", 10: "b"} 我需要的输出为: {2: "a", 10: "b", 8: "c", 6: "d", 3: "e", 9: "f", 5: "g", 4: "h", 7: "i"}
这里是我的JS代码:
var myObject = {2: "a", 3: "e", 4: "h", 5: "g", 6: "d", 7: "i", 8: "c", 9: "f", 10: "b"}

keysSorted = Object.keys(myObject).sort(function(a,b){return myObject[a]-myObject[b]});
console.log(keysSorted);

here is my fiddle: https://jsfiddle.net/j7yujL2y/


3
对象属性没有任何顺序……那有什么必要呢? - Pranav C Balan
@PranavCBalan,我的当前项目就是这样的,我不能改变对象...我只能按照这种方式进行排序。 - Akshay Kumar
8个回答

6

如果你只想对属性名称数组进行排序,那么可以使用String#localeCompare来比较字符串。

var myObject = {
  2: "a",
  3: "e",
  4: "h",
  5: "g",
  6: "d",
  7: "i",
  8: "c",
  9: "f",
  10: "b"
}

keysSorted = Object.keys(myObject).sort(function(a, b) {
  return myObject[a].localeCompare(myObject[b])
});
console.log(keysSorted);


1
关键字已经完美排序,但是值呢 :) - Akshay Kumar
@AkshayKumar:你需要根据已排序的键数组检索值... - Pranav C Balan

1

对象是无序映射,因此无法按顺序获取它们。
但是可以分别获取排序后的键和值。
它会像这样:

let myObject = {2: "Australia", 3: "China", 4: "Japan", 5: "Thailand", 6: "India", 7: "Malaysia", 8: "South Korea", 9: "Hong Kong", 10: "Taiwan", 11: "Philippines", 12: "Vietnam", 13: "France", 14: "United Kingdom", 15: "Germany", 16: "Sweden", 17: "Italy", 18: "Greece", 19: "Spain", 20: "Austria", 21: "The Netherlands", 22: "Belgium", 23: "Switzerland", 24: "United Arab Emirates", 26: "Ukraine", 27: "Russian Federation", 29: "Portugal", 30: "Saudi Arabia", 31: "Denmark", 33: "Norway", 34: "United States", 35: "Canada", 36: "Mexico", 38: "Poland", 39: "Czech Republic", 40: "Ireland", 41: "Bulgaria", 42: "Finland", 43: "Slovenia", 44: "Romania"}

let values = Object.keys(myObject).map(k => myObject[k]).sort();
let keys = Object.keys(myObject)
 .sort((a, b) => values.indexOf(myObject[a]) - values.indexOf(myObject[b]));

console.log(values, keys);


请查看 https://dev59.com/9Jzha4cB1Zd3GeqPCUX4#76346706 - Ahmad Ismail

0

奇怪的要求。

你的意思是需要console.log()的输出看起来像你写的那样吗?(我认为这是不可能的。:D)

为了完成这个特定的任务,尝试创建一个新的映射,将alpha作为键,数字作为值,虽然它看起来不完全符合你的要求。


0
您可以按特定顺序创建对象的ArrayMap

var myObject = {
  2: "a",
  3: "e",
  4: "h",
  5: "g",
  6: "d",
  7: "i",
  8: "c",
  9: "f",
  10: "b"
};
const keys = Array.from({
  length: Object.keys(myObject).length
});
let order = "abcdefghijklmnopqrstuvwxyz";
let arr = [];
const map = new Map();
for (let key in myObject) {
  arr[order.indexOf(myObject[key])] = {
    [key]: myObject[key]
  };
}
for (let prop of arr) {
  map.set(prop[Object.keys(prop)], prop)
}
for (let prop of map) {
  console.log(prop)
}
console.log(arr);


0
请尝试以下内容。这是 Pranav C Balan's answer 的修改版,可以同时返回键和值。

var myObject = {
        2: "a",
        3: "e",
        4: "h",
        5: "g",
        6: "d",
        7: "i",
        8: "c",
        9: "f",
        10: "b"
    }

keysSorted = Object.entries(myObject).sort(function (a, b) {
    return a[1].localeCompare(b[1]);
});
console.log(keysSorted);


0
var myObject = {
  2: "a",
  3: "e",
  4: "h",
  5: "g",
  6: "d",
  7: "i",
  8: "c",
  9: "f",
  10: "b"
}

keysSorted = Object.keys(myObject).sort(function(a, b) {
  return myObject[a] < myObject[b] ? -1 : 1
});
console.log(keysSorted);

0
 const myObject = {
  2: "a",
  3: "e",
  4: "h",
  5: "g",
  6: "d",
  7: "i",
  8: "c",
  9: "f",
  10: "b"
}

const keysSorted = Object.keys(myObject).sort(function(a, b) {
      return myObject[a].localeCompare(myObject[b])
    });
    console.log(keysSorted);

const sortedObj={};
keysSorted.forEach(key)={
//convert key to string because in JS,Object which has keys as number type are automatically sorted in ascending//
sortedObj[' '+key] = myObject[key];
})
console.log(sortedObj);

SyntaxError: Unexpected token '[' - Ahmad Ismail

0

你可以试试这个:

var keysSorted = Object.keys(myObject).sort(function (a, b) {
    return myObject[a].localeCompare(myObject[b])
});
var SortedList = [];
keysSorted.forEach(function (value) {
    SortedList.push(myObject[value]);
});

@AkshayKumar 你可以使用 SortedList 作为你的输出。 - Prasad Shigwan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接