如何在javascript中按第一个单词A然后B对字符串对象数组进行排序

如何在javascript中按第一个单词A然后B对字符串对象数组进行排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个对象数组,如下所示: 我怎样才能先按第一个词再按第二个词排序呢。比如: -Bulb: Vari-Fair -Octopus: Agile -Octopus: Energy Go 0: {id: 6, value: "Octopus: Agile"} 1: {id: 19, value: "EDF: EDF Economy 10 (SV)"} 2: {id: 20, value: "Octopus: Energy Go"} 3: {id: 21, value: "Igloo: Pio

我有一个对象数组,如下所示:

我怎样才能先按第一个词再按第二个词排序呢。比如:

-Bulb: Vari-Fair
-Octopus: Agile
-Octopus: Energy Go


0: {id: 6, value: "Octopus: Agile"}
1: {id: 19, value: "EDF: EDF Economy 10 (SV)"}
2: {id: 20, value: "Octopus: Energy Go"}
3: {id: 21, value: "Igloo: Pioneer"}
4: {id: 22, value: "Scottish Power: Smart Green EV"}
5: {id: 23, value: "OVO: EV Everywhere"}
6: {id: 24, value: "E.ON: Fix and Drive"}
7: {id: 25, value: "Tonik: Charge EV"}
8: {id: 26, value: "Engie: EV Home"}
9: {id: 27, value: "Good Energy: EV Tariff"}
10: {id: 28, value: "Ecotricity: Fully Charged"}
11: {id: 29, value: "Bulb: Vari-Fair"}
12: {id: 30, value: "Green Energy UK: TIDE"}
13: {id: 32, value: "Our Power: Economy 10"}

将值除以“:”并比较第一个值和第二个值:

const toSort = [
    {id: 6, value: "Octopus: Agile"},
    {id: 19, value: "EDF: EDF Economy 10 (SV)"},
    {id: 20, value: "Octopus: Energy Go"},
    {id: 21, value: "Igloo: Pioneer"},
    {id: 22, value: "Scottish Power: Smart Green EV"},
    {id: 23, value: "OVO: EV Everywhere"},
    {id: 24, value: "E.ON: Fix and Drive"},
    {id: 25, value: "Tonik: Charge EV"},
    {id: 26, value: "Engie: EV Home"},
    {id: 27, value: "Good Energy: EV Tariff"},
    {id: 28, value: "Ecotricity: Fully Charged"},
    {id: 29, value: "Bulb: Vari-Fair"},
    {id: 30, value: "Green Energy UK: TIDE"},
    {id: 32, value: "Our Power: Economy 10"}
]
const sorted = toSort.sort((a,b) => {
    const f = a.value.split(': ');
    const s = b.value.split(': ');
    if(f[0] > s[0])
        return 1;
    if(f[0] < s[0])
        return -1;
    if(f[1] > s[1])
        return 1;
    return -1;
});
console.log(sorted)
const toSort=[
{id:6,value:“Octopus:Agile”},
{id:19,值:“EDF:EDF经济10(SV)”},
{id:20,值:“章鱼:能量走”},
{id:21,value:“冰屋:先锋”},
{id:22,value:“苏格兰电力:智能绿色电动汽车”},
{id:23,值:“OVO:EV无处不在”},
{id:24,值:“E.ON:Fix-and-Drive”},
{id:25,值:“Tonik:Charge EV”},
{id:26,值:“Engie:EV Home”},
{id:27,值:“良好能源:电动汽车关税”},
{id:28,值:“Ecotricity:完全充电”},
{id:29,值:“Bulb:Vari Fair”},
{id:30,value:“绿色能源英国:潮汐”},
{id:32,value:“我们的力量:经济10”}
]
const sorted=toSort.sort((a,b)=>{
常数f=a.value.split(“:”);
常数s=b.value.split(“:”);
如果(f[0]>s[0])
返回1;
if(f[0]s[1])
返回1;
返回-1;
});
console.log(已排序)
您可以执行以下操作:

var-arr=[
{id:6,value:“Octopus:Agile”},
{id:19,值:“EDF:EDF经济10(SV)”},
{id:20,值:“章鱼:能量走”},
{id:21,value:“冰屋:先锋”},
{id:22,value:“苏格兰电力:智能绿色电动汽车”},
{id:23,值:“OVO:EV无处不在”},
{id:24,值:“E.ON:Fix-and-Drive”},
{id:25,值:“Tonik:Charge EV”},
{id:26,值:“Engie:EV Home”},
{id:27,值:“良好能源:电动汽车关税”},
{id:28,值:“Ecotricity:完全充电”},
{id:29,值:“Bulb:Vari Fair”},
{id:30,value:“绿色能源英国:潮汐”},
{id:32,value:“我们的力量:经济10”}
];
var result=arr.sort((a,b)=>a.value.localeCompare(b.value))//utf8支持

控制台日志(结果)您试图执行的操作是什么?@Sergey这与此无关,因为问题是所有对象都是一个字符串。您可以将字符串拆分为部分,然后在排序中进行比较