1

I am completely newbie in JavaScripts and sorry for my english if it so bad. I have json file looking like that:

{
   "posts":[
      {
         "id":2236659,
         "updated_at":"2020-05-02T19:58:43.763-04:00",
         "file":{
            "width":933,
            "height":1200,
            "ext":"png",
            "size":1325351,
            "md5":"d1f501df73f7d1daec07a86657baae01"
         }
      },
      {
         "id":2227726,
         "created_at":"2020-04-23T08:06:37.907-04:00",
         "file":{
            "width":933,
            "height":1200,
            "ext":"png",
            "size":1182791,
            "md5":"112cadaaaa89841e8bb7633ba272a409"
         }
      },
      {
         "id":2218681,
         "created_at":"2020-04-16T07:56:56.849-04:00",
         "file":{
            "width":933,
            "height":1200,
            "ext":"png",
            "size":1241188,
            "md5":"c3c13b8e5c72913fa7db03ffc8b6f3c4"
         }
      }
   ]
}

The problem that the order of posts is incorrect. It sorted by ascending of id. Real json file contain more than 3 post. I have order list. How I can sort it with custom order that I typed. For example: I want get md5 with this order: 2227726,2218681,2236659

2
  • so you need them to be ordered according to MD5 and not the id? Commented May 3, 2020 at 1:38
  • No by id via id order list Commented May 3, 2020 at 1:40

2 Answers 2

1

I hope now I got it right :)

const md5s = listOfIds.map(id => data.posts.find(post => post.id === id))

where listOfIds is the list of ids like [2149825,2149832,2149838...]

The result is array of md5's in the order specified in listOfIds array

Sign up to request clarification or add additional context in comments.

11 Comments

In my answer the posts array sorted the way the ids of posts are 2227726,2218681,2236659 as you suggested. If you want to sort the array by ids in an ascending order, just use data.posts.sort((a,b) => a.id - b.id)
I need to understand what do you need exactly. Do you want to sort the posts by their id or md5? And in which order, asc or desc?
What is your runtime environment? Is it browser or Node.js? Maybe you should try console.log(yourObjectName)? It seems like you're trying to use 'alert' function that represents objects as string.
I can post a full script if you specify what order you exactly need, on which parameter of your Post object the sorting needs to be applied and in which order, asc or desc :)
Yes. It work. I use for ( x=0 ; x< posts.length;x++) { document.write(md5[x].file.md5) . I accidentally deleted x why it not wanted find md5. sorry for taked to much time all work nice now. Google translate don't translate correct from my language.
|
1

basically you can use the sort function, it would like something like this:

// expected result 2227726,2218681,2236659
const data = {
  "posts": [{
      "id": 2236659,
      "updated_at": "2020-05-02T19:58:43.763-04:00",
      "file": {
        "width": 933,
        "height": 1200,
        "ext": "png",
        "size": 1325351,
        "md5": "d1f501df73f7d1daec07a86657baae01"
      }
    },
    {
      "id": 2227726,
      "created_at": "2020-04-23T08:06:37.907-04:00",
      "file": {
        "width": 933,
        "height": 1200,
        "ext": "png",
        "size": 1182791,
        "md5": "112cadaaaa89841e8bb7633ba272a409"
      }
    },
    {
      "id": 2218681,
      "created_at": "2020-04-16T07:56:56.849-04:00",
      "file": {
        "width": 933,
        "height": 1200,
        "ext": "png",
        "size": 1241188,
        "md5": "c3c13b8e5c72913fa7db03ffc8b6f3c4"
      }
    }
  ]
}

const customComparer = {
  2227726: 1,
  2218681: 2,
  2236659: 3
}

const sortFn = (a, b) => {
  return customComparer[a.id] - customComparer[b.id]
}
const result = data.posts.sort(sortFn)

console.log(result)

notice that if you want this to be scalable, this is not the right way to sort an array, you are trying to sort it in a not a regular way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.