## sort two completly different type of array with one common properties

array sort

javascript sort multiple criteria

javascript sort multidimensional array multiple columns

javascript sort two dimensional array numerically

sort multiple arrays javascript

array with multiple properties

javascript sort array of objects by key value

Hi I have two array with different properties and only properties has common `sort_order`

and I want to merge this two array and want to sort this two array by `sort_order`

.

Here is the code:

var array1:[AnyObject] = [{ "name":"obj1Array1" "sort_order": 4 "type":"G" },{ "name":"obj2Array1" "sort_order": 1 "type":"G" },{ "name":"obj31Array1" "sort_order": 5 "type":"G" }] var array2:[AnyObject] = [{ "Group":"M" "sort_order": 3 "type":"G" },{ "Group":"C" "sort_order": 2 "type":"G" },{ "Group":"D" "sort_order": 6 "type":"G" }]

Ah, I guess, in good old Objective-C days, we could have used a sort predicate. Well, those days are behind us and protocols are ministers in new Swift government.

Lets see what protocols can do in this case. You could define a protocol that has sort_order as a property. This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic). Then you can create a merged array of type Ordered, and use sort function on that array.

protocol Ordered { var sort_order:Int{get} } struct A:Codable, Ordered { let name:String let sort_order:Int let type:String } struct B:Codable, Ordered { let Group:String let sort_order:Int let type:String } var array1:[A] = [] var array2:[B] = [] var mergedArray:[Ordered] = array1 for item in array2 { mergedArray.append(item) } mergedArray.sort { (A, B) -> Bool in return A.sort_order < B.sort_order }

**Sort Two Arrays of Different Types into One,** var arrayA = [TypeA(1), TypeA(3), TypeA(5)] var arrayB = [TypeB(2), Put all common properties into a protocol, the build and sort an array of Sort an array containing two types of elements We are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.

Use this

var array1 : [[String : Any]] = [[ "name" : "obj1Array1", "sort_order" : 4, "type":"G" ], [ "name":"obj2Array1","sort_order": 1,"type":"G"], ["name":"obj31Array1","sort_order": 5,"type":"G"]] var array2 : [[String : Any]] = [[ "Group":"M", "sort_order": 3,"type":"G"], ["Group":"C", "sort_order": 2,"type":"G"], ["Group":"D","sort_order": 6,"type":"G"]] let newArr = [array1, array2].flatMap({$0}).sorted(by: { ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int) })

**Sophisticated Sorting in JavaScript,** Using sort it's possible to organise arrays, not just into alphabetical or numerical order, if the function returns zero, leave a and b unchanged with respect to each other It's using the word “index” in a very confusing way; how I've sort, but if the two values are equal, then use value [1] for secondary sort. A sorting algorithm takes an array as input and outputs a permutation of that array that is sorted. There are two broad types of sorting algorithms: integer sorts and comparison sorts.

Here how you can do it using swift 4-

var array1: [[String: Any]] = [ ["name": "obj1Array1", "sort_order": 4, "type": "G"], ["name": "obj2Array1", "sort_order": 1, "type": "G"], ["name": "obj3Array1", "sort_order": 5, "type": "G"] ] var array2: [[String: Any]] = [ ["Group": "M", "sort_order": 3, "type": "G"], ["Group": "C", "sort_order": 2, "type": "G"], ["Group": "D", "sort_order": 6, "type": "G"] ] let mergedArray = array1 + array2 let sortedAndMerged = mergedArray.sorted{ ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int) } print(sortedAndMerged) // prints [["type": "G", "name": "obj2Array1", "sort_order": 1], ["Group": "C", "sort_order": 2, "type": "G"], ["Group": "M", "sort_order": 3, "type": "G"], ["type": "G", "name": "obj1Array1", "sort_order": 4], ["type": "G", "name": "obj3Array1", "sort_order": 5], ["Group": "D", "sort_order": 6, "type": "G"]]

**Learn Objective-C for Java Developers,** Sort descriptors are instances of NSSortDescriptor, and are essentially a property a sort descriptor compares a common property of the two objects obtained The collection's -sortUsingDescriptors: method takes an array of NSSortDescriptor Various views that display collections of objects in columns and rows typically Minimum number of swaps required to sort an array of first N number; Efficiently merging two sorted arrays with O(1) extra space and O(NlogN + MlogM) Find XOR of all elements in an Array; Split the given string into Primes : Digit DP; Number of pairs in an array with the sum greater than 0; Count of subsets with sum equal to X using Recursion

You can try like this

let array1: [[String:Any]] = [ [ "name":"obj1Array1", "sort_order": 4, "type":"G" ], [ "name":"obj2Array1", "sort_order":1, "type":"G" ], [ "name":"obj31Array1", "sort_order":5, "type":"G" ]] let array2: [[String:Any]] = [ [ "Group":"M", "sort_order": 3, "type":"G" ], [ "Group":"C", "sort_order": 2, "type":"G" ], [ "Group":"D", "sort_order": 6, "type":"G" ] ] var new = array1 + array2 new = new.sorted{ ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int) } print(new)

**Flash 8: Projects for Learning Animation and Interactivity: ,** Once a variable is defined, you can also use brackets to set or get an array's values, The most common rationale is that an array index describes the number of of this writing, one property and a dozen methods were used to work with arrays the last item in the sorted, two-item example array, leaving only “ActionScript. In non-comparison based sorting, elements of array are not compared with each other to find the sorted array. Radix sort – Best, average and worst case time complexity: nk where k is the maximum number of digits in elements of array. Count sort – Best, average and worst case time complexity: n+k where k is the size of count array. Bucket sort –

**A New Companion to Chaucer,** genres (which he calls complex) are defined by several characteristics operating whereas Emily Steiner uses four quite different types of interacting characteristics – form, subject, sorted” and “theorists of genre differ most radically precisely on this one point, on the 'things' in In other contexts a plenitudinous array may Sorting an array according to another array using pair in STL; Median of sliding window in an array; Minimum number of swaps required to sort an array of first N number; Efficiently merging two sorted arrays with O(1) extra space and O(NlogN + MlogM) Number of pairs in an array with the sum greater than 0

**array_multisort - Manual,** array_multisort — Sort multiple or multi-dimensional arrays Sorting type flags: Add $data as the last parameter, to sort by the common key uses PHP's array_multisort function for sorting; In order to sort by one field only (yet still have the other array(s) being correspondingly sorted), one can use array_keys (which This will sort your array by "one" and if they are the same (ie. 0) then it will sort by "two". It's simple, concise, readable, and best of all - works perfectly.

**Array methods,** Starting from the index 1 it removed 1 element. In the next example we remove 3 elements and replace them with the other two:. but you're making headway; you're creeping up on the idea of making a two-item custom data type, storing objects of that in a list, and then sorting that list, here. that would be one way of making a "two-column list", and possibly the most direct one to get functionality equivalent to that description.

##### Comments

- if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this:
`name`

from array1 is the same as`group`

from array2? - because I already created model classes for this objects
- For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say
`Any`

, how would you know which object is of which type when you want to access them from the said array. - you should be able to achieve that using protocol