0

Sorting & arrange list of object inside object on the basis of order key

const listing = {
   "football":{
      "name":"FOOTBALL",
      "slug":"football",
      "arr":[],
      "order": 4
   },
   "table-tennis":{
      "name":"TABLE TENNIS",
      "slug":"table-tennis",
      "arr":[],
      "order": 1
   },
   "cricket":{
      "name":"CRICKET",
      "slug":"cricket",
      "arr":[],
      "order": 5
   },
   "badminton":{
      "name":"BADMINTON",
      "slug":"badminton",
      "arr":[],
      "order": 2
   },
   "hockey":{
      "name":"HOCKEY",
      "slug":"hockey",
      "arr":[],
      "order": 3
   }
}
Object.keys(listing).sort().map((item) => {
  return (
    <div>
      <div onClick={() => sportsKitFilter()}>{listing[item].name}</div>
    </div>
  )
})

Actual Result:-

BADMINTON
CRICKET
FOOTBALL
HOCKEY
TABLE TENNIS

Expected Result:-

TABLE TENNIS
BADMINTON
HOCKEY
FOOTBALL
CRICKET

So, In the expected result, Storing is on the basis of order key

Guide me, In this scenerio??

Sagar
  • 134
  • 8
  • `sort`'s default sort order is ascending. So the output is expected. – evolutionxbox Jan 05 '22 at 16:08
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – evolutionxbox Jan 05 '22 at 16:08
  • Have you made any attempt to do this yourself? The code above just uses `sort` with no arguments, so it's going to do a naive code-unit-based lexicographic (loosely, alphabetic) sort. – T.J. Crowder Jan 05 '22 at 16:09
  • I think this is already answered [here](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Muhammad Abdullah Jan 05 '22 at 16:11
  • 1
    @MuhammadAbdullah - Not really, the OP here isn't trying to sort an object, just an array (which, granted, came from `Object.keys`). But not a million miles off. – T.J. Crowder Jan 05 '22 at 16:12
  • You know how you got the name by doing `listing[item].name`? (That really should be `key` or `name`, not `item`.) Do that same thing with the two arguments the `sort` callback receives (which are two keys from the array from `Object.keys`), in order to get the `order` property for each of them, then subtract. – T.J. Crowder Jan 05 '22 at 16:13
  • Not Getting expected result with all your suggestion – Sagar Jan 05 '22 at 16:31

2 Answers2

3

Currently i am sorting listing.order. try this code it's working !

 Object.keys(listing).sort((a, b) => listing[a].order - listing[b].order).map((item) => {
      return (
        <div>
          <div onClick={() => sportsKitFilter()}>{listing[item].name}</div>
        </div>
      )
    })
Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24
1

How's this for you?

const listing = {
  football: {
    name: "FOOTBALL",
    slug: "football",
    arr: [],
    order: 4,
  },
  "table-tennis": {
    name: "TABLE TENNIS",
    slug: "table-tennis",
    arr: [],
    order: 1,
  },
  cricket: {
    name: "CRICKET",
    slug: "cricket",
    arr: [],
    order: 5,
  },
  badminton: {
    name: "BADMINTON",
    slug: "badminton",
    arr: [],
    order: 2,
  },
  hockey: {
    name: "HOCKEY",
    slug: "hockey",
    arr: [],
    order: 3,
  },
};

const ordered = Object.keys(listing).sort(
  (a, b) => listing[a].order - listing[b].order
).map(item => ["YOUR JSX HERE"]);