-1

I'm trying to sort an array of objects in JS, but it doesn't work for some reason. The array I requested from API may contain numbers, upper and lower case letters, and some Chinese characters.

eg:

const arr = [
{ "title": "!Test Segment and CSV" }, 
{ "title": "&test (SC)" }, 
{ "title": "1234test (SC)" }, 
{ "title": "AAShop1 (SC)" }, 
{ "title": "AAShop2(SC)" }, 
{ "title": "ABCshop123 (SC)" },
{ "title": "adidas Outlet" },
{ "title": "AIGLE" }, 
{ "title": "American Eagle" },
{ "title": "Châteraisé" }, 
{ "title": "Tekcent" }, 
{ "title": "반찬 & COOK" },
{ "title": "始祖鸟" }, 
{ "title": "春水堂" }, 
{ "title": "稻成" }
];

I want it sorted according to the following rules.

  1. sort a after A

  2. sort B after A

  3. sort Ac after ABC

     { "title": "!Test Segment and CSV" }, 
     { "title": "&test (SC)" }, 
     { "title": "1234test (SC)" }, 
     { "title": "AAShop1 (SC)" }, 
     { "title": "AAShop2(SC)" }, 
     { "title": "ABCshop123 (SC)" },
     { "title": "AIGLE" }, 
     { "title": "American Eagle" },
     { "title": "adidas Outlet" },
     { "title": "Châteraisé" }, 
     { "title": "Tekcent" }, 
     { "title": "始祖鸟" }, 
     { "title": "春水堂" }, 
     { "title": "稻成" },
     { "title": "반찬 & COOK" }
    

Thanks in advance for any help!

Echo
  • 9
  • 2
  • 1
    How do you expect `A` and `a` to get sorted differently, when you apply `toLowerCase` to both of them? You are effectively testing `'a' > 'a'` now. – CBroe Jul 11 '22 at 10:45
  • Noted and thanks for reminding. – Echo Jul 13 '22 at 09:59

3 Answers3

2

.sort() and .localeCompare(). Note, in the OP, the input array is already sorted correctly so in this answer I have jumbled the order of the input array to demonstrate that the code functions correctly.

const data = [{ "title": "BB" }, { "title": "Ac" }, { "title": "adidas" }, { "title": "AA" }, { "title": "Ba" }, { "title": "ABC" }];

const output = data.sort((a, b) => a.title.localeCompare(b.title));

console.log(output);
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • The question asks for 'A' to come before 'a', but localCompare appears to do the opposite. However, for the other requirements it seems to work. – Yogi Jul 11 '22 at 11:02
  • @Yogi "*The question asks for 'A' to come before 'a', but localCompare appears to do the opposite.*" that's the order I'm seeing here - first is "AA" then "adidas" is fourth. Tried it in both Firefox and Chrome. Where are you seeing the opposite order? – VLAZ Jul 11 '22 at 11:09
  • The array I requested from API may contain numbers, upper and lower case letters, and some Chinese characters. I already tried your method, but failed. – Echo Jul 13 '22 at 10:11
0

You just need to sort it on lower case.

    let myArr = [{ "title": "AA" }, { "title": "ABC" }, { "title": "Ac" }, { "title": "adidas" }, { "title": "Ba" }, { "title": "BB" }]
    let sortList = myArr.sort(function (a, b) {
        return a.title.toLowerCase() > b.title.toLowerCase()
    })
    console.log(sortList)
Mina
  • 14,386
  • 3
  • 13
  • 26
  • [Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?](https://stackoverflow.com/q/24080785) – VLAZ Jul 11 '22 at 11:01
  • I've updated my question and it would be great if you could help answer it. thanks. – Echo Jul 13 '22 at 10:51
0

You can simply achieve that without manipulating a string into a lowerCase with the only help of a simple Array.sort() method.

Live Demo :

let myArr = [{ "title": "AA" }, { "title": "ABC" }, { "title": "Ac" }, { "title": "adidas" }, { "title": "Ba" }, { "title": "BB" }];

let sortedArr = myArr.sort((a, b) => {
    return a.title - b.title
});

console.log(sortedArr);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • I've updated my question and it would be great if you could help answer it. thanks. – Echo Jul 13 '22 at 10:52