0

I need to sort the object based on 3rd key , here is my boject

a = [["AED",1,1],
    ["AFN",22.08086,1],
    ["ALL",27.171554,0],
    ["AMD",105.058803,1], 
    ["ANG",0.487406,0],
    ["AOA",227.493716,0],
    ["ARS",95.308373,0],
    ["AUD",0.420237,0],
    ["AWG",0.487406,0],
    ["AZN",0.462914,0],
    ["BAM",0.490727,0],
    ["BBD",0.544588,1]]

required output

a = [["AED",1,1],
    ["AFN",22.08086,1],
    ["AMD",105.058803,1],
    ["AMD",105.058803,1],
    ["ALL",27.171554,0],
    ["ANG",0.487406,0],
    ["AOA",227.493716,0],
    ["ARS",95.308373,0],
    ["AUD",0.420237,0],
    ["AWG",0.487406,0],
    ["AZN",0.462914,0],
    ["BAM",0.490727,0],
    ["BBD",0.544588,0]]

So based on final key that is 1 0r 0 it should be sorted

derpirscher
  • 14,418
  • 3
  • 18
  • 35
vellai durai
  • 1,019
  • 3
  • 16
  • 38
  • Use this function, please: a.sort((item1, item2) => item1[0] > item2[0]) – happy smile Sep 01 '23 at 07:00
  • 1
    @happysmile have you tried your suggestion? does it lead to the desired result? Most certainly not because (1) it should be sorted by *last* element of the inner array and (2) the callback of `sort` must return a *number* and not a *boolean* – derpirscher Sep 01 '23 at 07:19
  • @vellai, sorry, please try to use with this code. I tried it on my side and it seems correct. code: a.sort((a, b) => a[2] > b[2] ? -1 : a[2] === b[2] ? 0 : 1) – happy smile Sep 01 '23 at 08:30

0 Answers0