-1

I have a list of data which contains some dicts.

data = []
data[0]['id']=5  data[0]['title']=''
data[1]['id']=6  data[1]['title']=''
data[2]['id']=4  data[2]['title']=''
data[3]['id']=0  data[3]['title']=''
data[4]['id']=9  data[4]['title']=''
data[5]['id']=1  data[5]['title']=''

Yep, I fixed everything. Thank you. What about cookies? How can I save my list in JS cookies?

  • sqlite? if so do sort with an `ORDER by id` clause in the query – Lawrence Cherone Jun 04 '21 at 16:38
  • 2
    besides, I think this question may be a dupe. [Sort array by * (alphabetically) in Javascript](https://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript), [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Lawrence Cherone Jun 04 '21 at 16:46

2 Answers2

1

Sorting can be done like this:

data.sort((a, b) => a.id - b.id);
Vladislav
  • 88
  • 4
0

Array.prototype.sort is your friend here. Your Array will be mutated this helper method so you may want to copy it first.

let data = [], ids = [5,6,4,0,9,1]
for (let i = 0; i < ids.length; i++) {
  data.push({id: ids[i], title: ''})
}
const sorted = data.slice(0).sort((a,b)=>a.id - b.id)
apena
  • 2,091
  • 12
  • 19