Reorder array based on passed in value

javascript sort array of objects alphabetically
javascript sort array of objects by key value
sort array of objects javascript
sort nested array of objects javascript
typescript sort array of objects
angular 6 sort array of objects by property
javascript sort array of objects by number
sort array of arrays javascript

If I have this array:

["A", "B", "C", "D"]

I have this string:


I want to reorder the array to look like this:

["C", "A", "B", "D"]

So, the passed in string will move the matching element to the front of the list.

a = ['A', 'B', 'C', 'D', 'C']
target = 'C'

a.partition { |e| e==target }.reduce(:+)
  #=> ["C", "C", "A", "B", "D"]

or { |e| e==target }.concat(a.reject { |e| e==target })
  #=> ["C", "C", "A", "B", "D"]

a is not modified.

Javascript - sort array based on another array, array sort function, you can pass in a custom comparator to be used when sorting the array. The comparator should return a negative number if the first value  The syntax of Array.splice is: yourArray.splice(index, howmany, element1, /*..,*/ elementX); Where: index is the position in the array you want to start removing elements from; howmany is how many elements you want to remove from index; element1, , elementX are elements you want inserted from position index.

If you actually want to sort the array, this would work:

a = ['A', 'B', 'C', 'D']
a.sort_by.with_index { |e, i| [e == 'C' ? 0 : 1, i] }
#=> ["C", "A", "B", "D"]

Otherwise, you can just delete 'C' from the array and prepend it:

#=> ["C", "A", "B", "D"]

Note that this will modify a.

JavaScript Array sort: Sorting an Array More Effectively, This tutorial shows you how to use the JavaScript array sort method to sort arrays of To fix this, you need to pass a comparison function to the sort() method. The sort() method will sort elements based on the return value of the compare()  The employees array above is an array of objects with properties of different data types, from string, numeric, to a date string. The sort() method can be used to sort the array based on the values of one of these properties, such as sorting the array by name, age, or even when they retire. The basic idea is to modify the compare function so it compares the desired properties' values.

In case you have non uniq target string:

ary = ["A", "B", "C", "D", "C"]
target = "C"

ary.count(target){ target } + ary.reject{ |e| e == target }
#=> ["C", "C", "A", "B", "D"]

I must find a more elegant way...

ary.find_all{ |e| e == target } + ary.tap { |a| a.delete target }

How to sort an array numerically in JavaScript, How do you sort an array according to another array? Sort(Array, Array): Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array.

Does .sort function change original array?, The sort() method can be used to sort the array based on the values of one of any comparison function passed in directly on the array, such as myarray.sort() . Get the highest value in an array: var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b-a}); // Sort the numbers in the array in descending order

Sorting an array of objects, And if it returns 0 no sorting is necessary. When you pass sort a comparison function, that function will be called repeatedly and given different values from the​  Any values assigned to an observable array will be default passed through observable to make them observable. Create a shallow array to disable this behavior and store a values as-is. Create a shallow array to disable this behavior and store a values as-is.

Reordering Arrays with Array#sort, If the items are arrays, blobs, functions, objects and/or tables, they will be sorted by reference not value. Sort strings based on the numeric value in the table. The return value from the comparer function basically identifies the sort order of subsequent array elements. Summarized as the following: If ComparingFunction(a, b) is < 0, then a will be sorted to a lower index than b.

  • Is the input array always sorted? What if there are multiple matching elements? Are all elements unique? Thanks.
  • Good question, for this particular case, there can't be. Yes, all elements are unique.
  • What is your desired result when the array is ["A", "B", "C", "D", "C"] and the string is "C"? Aside: don't forget to include @username in your comments so the intended recipient will be notified by SO.
  • That's what I was looking for! a.partition { |e| e==target }.reduce(:+)