Skip to content

Tag: sorting

Grouping array of objects by multiple keys

I feel embarrassed for asking this question as I should know how to figure it out, but I’m spinning my wheels on grouping an array of objects by multiple keys. Here’s the data: What I’ve been trying to generate with little success is the following: The schema is: I’ve tried the followi…

Using hasOwnProperty() on an array

Can I use hasOwnProperty() on an array? I have this array of RGBA values: I’d like to create an object that sorts them in order of their frequency in the array. I’m trying a combination of things. First I thought to convert the array into an object using: But that returns something like this: So I…

Sorting multiple arrays based on one

I have 2 arrays: the first one contains a list of score numbers, and the second contains a list of countries with the same number of elements. Each country is linked with its proper score during the input. So, let’s say the input arrays are the following: I need the following output: A possible solution…

Sort array on key value

I have a function which sorts by name currently and an array of value / key pairs. I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so: Answer [edit 2020/08/14] This was rather an old answer and not very good as well, so simplified and

Javascript – sort array based on another array

Is it possible to sort and rearrange an array that looks like this: to match the arrangement of this array: Unfortunately, I don’t have any IDs to keep track on. I would need to priority the items-array to match the sortingArr as close as possible. Update: Here is the output I’m looking for: Any idea how this…

Sorting in Javascript with special characters

I have an array with the following values and i want to sort it in javascript in the following way in to three parts. word starting from special character word starting from digit word starting from alphabets. So this should be the sequence of the sorted array. EDIT: Here’s a function that I’ve be…

Javascript sort function. Sort by First then by Second

I have an array of objects to sort. Each object has two parameters: Strength and Name I want to sort first by Strength and then by name alphabetically. I am using the following code to sort by the first parameter. How do I sort then by the second? Thanks for your help. (I am using Array.sort() with the aforem…