Sort an array based on another array element’s class

sort array based on another array javascript
sort an array based on another array python
sort array based on another array php
sort array based on another array swift
sort array based on another array c#
sort array based on another array lodash
sort array based on another array matlab
sort array of objects javascript

I am looking for way to sort elements in an array based on the classes of the elements in another array. I have:

order = [String, Integer, NilClass ,TrueClass]
arry = [1, 2, 3, 4, 5, true, false, nil, 34, nil, 'Hello', 'World']

In order, the order of the elements is dynamic. The result should be:

result_arry = ['Hello', 'World', 1, 2, 3, 4, 5, 34, nil, nil, true, false]

How can I sort arry based on the class of the elements in order?

I tried:

hash = arry.group_by  {|n| n.class } 
# => {Fixnum=>[1, 2, 3, 4, 5, 34], TrueClass=>[true], FalseClass=>[false], NilClass=>[nil, nil], String=>["Hello", "World"]}

I think this works:

arry.sort_by{ |v|
    order.map{ |c|
        v.is_a?(Object.const_get(c)) ? -1 : 1
    }
}

# ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]

Object.const_get(c) converts the string to class.

Sort an array according to the order defined by another array , For the elements not present in A2, append them at last in sorted order. Create another array visited[] and initialize all entries in it as false. visited[] is used to A Binary Search based function to find index of FIRST occurrence class GFG {. Sort an array according to the order defined by another array Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order.


Using group_by is a good approach, because it keeps the element's order:

hash = arry.group_by(&:class)
#=> {
#     Integer => [1, 2, 3, 4, 5, 34],
#     TrueClass => [true],
#     FalseClass => [false],
#     NilClass => [nil, nil],
#     String => ["Hello", "World"]
#   }

We can use sort_by to sort the hash by its keys based on each key's index in the order array. If a class is missing from orders, we use the array's size as a fallback to have it sorted last:

sorted = hash.sort_by { |k, _| order.index(k) || order.size }
#=> [
#     [String, ["Hello", "World"]],
#     [Integer, [1, 2, 3, 4, 5, 34]],
#     [NilClass, [nil, nil]],
#     [TrueClass, [true]],
#     [FalseClass, [false]]
#  ]

Finally, flat_map extracts each element's last part:

sorted.flat_map(&:last)
#=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]

You can also use sort_by without prior grouping, but it may shuffle elements with the same class:

arry.sort_by { |e| order.index(e.class) || order.size }
#=> ["World", "Hello", 3, 4, 5, 1, 2, 34, nil, nil, true, false]

This is because sort_by is not stable.

In order to fix it, we have to take their indices into account:

arry.sort_by.with_index { |e, i| [order.index(e.class) || order.size, i] }
#=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]

If you also want to match subclasses (e.g. make Integer match Fixnum in older Ruby versions), you have to pass a block to index:

order.index { |cls| e.is_a?(cls) }

Order an array of objects based on another array order · GitHub, Order an array of objects based on another array order how can we move object that not found in item_order eg: {id: 6, label: 'Six') to be on the Your change does not add new elements at the END of the array but rather at the BEGINNING. sort() method is a java.util.Arrays class method.. Syntax: public static void sort(int[] arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value.


Sorting, per se, is not necessary.

missing_classes = arry.map(&:class) - order
  #=> [Symbol, FalseClass]
arry.group_by(&:class).values_at(*(order + missing_classes)).flatten
  #=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, :cat, false]

Custom Sort, Sort array according to another array: Given two arrays, reorder elements of first array by order of elements defined by the second array. Elements which are not Comparator;. class CustomComparator implements Comparator<Integer>. Sorting Java ArrayList. ArrayList is one of the most commonly used collection classes of the Java Collection Framework because of the functionality and flexibility it provides. ArrayList is a List implementation that internally implements a dynamic array to store elements.


JavaScript Array sort: Sorting Array Elements, By default, the sort() method sorts the array elements in ascending order with the smallest The sort() method will sort elements based on the return value of the To resolve this, you use the localeCompare() method of the String object to  So 0.4049 corresponds to 1, 0.1354 corresponds to 2, 0.0914 corresponds to 3 etc. I want A to be sorted in decreasing order and B to be in the order that A has come out in. Sorry I don't know if that is clear.


Indexed collections, However, you can use the predefined Array object and its methods to work with for manipulating arrays in various ways, such as joining, reversing, and sorting them. You can populate an array by assigning values to its elements. The forEach() method provides another way of iterating over an array: Custom Sort | Sort array based on another array. Given two arrays, reorder elements of first array by order of elements defined by the second array. Elements which are not present in the second array but present in first array should be appended in the end sorted. Second array can contain some extra elements which are not part of first array.


Array.prototype.sort(), The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting  Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows: Here we will discuss the first 4 methods. This method sorts the elements in an Array using the IComparable<T> generic interface implementation of each element of the Array.