Skip to content
Advertisement

Tag: sorting

Sort the array of database object-id

I have a array of mongodb object-id and i want to sort them in such a way that similar IDs are next to each other example : Input : var array = [“507f191e810c19729de860ea”,”00000020f51bb4362eee2a4d”,” 507f191e810c19729de860ea”] Output : var array = [“507f191e810c19729de860ea”,”507f191e810c19729de860ea”,”00000020f51bb4362eee2a4d”] Answer Simply sorting the input array would result in the same input array being sorted in natural order (converting the

Merge sort time complexity check

Hello guys I was learning about the merge sort so I wrote this function that takes 2 arrays and merges them sorted. Can someone tell me the time complexity of this function? I thought it would be O(n^2) as I am using shift inside a while loop. Is that correct or am I missing something here? Answer The worst case

How to sort prompt answer by price

I’m having a small project where I have to do a shopping list (for fruit salad) which allows user to add products and set prices for them. I’ve managed to do this so far that the “final” prompt shows all added products and prices for the user. Problem is that it should show all this from lowest to the highest

JavaScript: Comparing Two objects in one array

I’m new to this world of vanilla javascript. I’ve been trying complete some challenges but it seems like I’m unable to find the solution to a challenge. The task is:”Sort array by object property” Write a function that takes an array of objects as an argument Sort the array by property b in ascending order Return the sorted array Expected

Javascript – Sort array of objects by property date

I have the following response from a service: To sort it by date I do the following: But the value of “sortDate” is not correct: Should be: How can i solve it?, thanks Answer The problem is that the comparison function is supposed to return a positive number for if a should be after b, negative if b should be

Array.sort() on multiple properties

I have an array, productData with multiple properties. This is how I currently sort the array by monthlyCost, ascending. How can I modify this to sort all items by a boolean property isPromoted, followed by monthlyCost? My array should start with all items where isPromoted == true sorted by monthlyCost, then all items where isPromoted == false sorted by monthlyCost.

Advertisement