-1

I am developing a web site by using react and javascript.

I have an array like this

const array = [
{id: 981, name: 'aaa', part: 'January'},
{id: 108, name: 'bbb', part: 'April'},
{id: 205, name: 'ccc', part: 'Febraury'},
{id: 215, name: 'ddd', part: 'March'},
]

Now i want to make it sorting by part using Javascript array method sort. like this

const array = [
    {id: 981, name: 'aaa', part: 'January'},
    {id: 205, name: 'ccc', part: 'Febraury'},
    {id: 215, name: 'ddd', part: 'March'},
    {id: 108, name: 'bbb', part: 'April'}
    ]

i do not care about id or name, just want to sort it only by part. Is there anyone help me to code this?

Yoon Zoy
  • 59
  • 4
  • Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Please [search thoroughly](/search?q=%5Bjs%5D+sort+array+objects) before posting. More about searching [here](/help/searching). – T.J. Crowder May 08 '22 at 11:30
  • I’m assuming you actually want to sort by month value in part and not just the string value of the month. Create an array or an object containing a mapping between the string months and their index/numeric value. Then simply use the JavaScript sort method to specify your own function where you convert each object’s part month into a number and do the comparison based on that – Azarro May 08 '22 at 11:32
  • I don't agree the referenced question will necessarily help, other than in the obvious way that you need a custom sort function. The trick you will need inside the sort function is to be able to decide which months come before which others. One way is to define an array with the months ordered ` "["January", "February", ... ... "November", "December"]` and test your current `.part` againt it using `indexOf(element.part)` – Dave Pritlove May 08 '22 at 11:34
  • Yup +1 to Dave that is the right solution. This question was closed wrongly. – Azarro May 08 '22 at 11:36
  • Thank you guys I don't know how to re-open my questions ... so i need to make a new array about month values only so that can help to compare and sort the original array right? – Yoon Zoy May 08 '22 at 11:39
  • 2
    @YoonZoy yes, if you study typical sort functions you will be able to adapt them if you get the index of your current month on the months array you build. Watch out for case-sensitivity - it might be worth including a `.toLowerCase()` in each side of your comparison as "january" will otherwise not match "January". @Azarro thanks, I was responding while you were making the same point. Closing decisions often seem to be made without considering whether the referenced question is really helpful for the current problem. Pity. – Dave Pritlove May 08 '22 at 11:43
  • The main problem with your question is: there is no attempt. This is a reason to downvote a question. Without attempt most questions are too broad or unclear. Without the attempt the duplicate is valid because it's unclear what you're struggling with. You should read the [tour] _"Don't ask about... Questions you haven't tried to find an answer for (show your work!)"_ Another problem is [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – jabaa May 08 '22 at 11:46

0 Answers0