How do I sort an array of structs by multiple values?

Aaron Bratcher

I already have code to sort by 1 value as shown below, but I'm wondering how to sort using multiple values? I would like to sort by set and then by someString.

One is an integer, and one is a string in this case. I had considered converting the integer to a string and then concatenating them, but thought there must be a better way because I may have 2 integers to sort by in the future.

struct Condition {
    var set = 0
    var someString = ""
}

var conditions = [Condition]()

conditions.append(Condition(set: 1, someString: "string3"))
conditions.append(Condition(set: 2, someString: "string2"))
conditions.append(Condition(set: 3, someString: "string7"))
conditions.append(Condition(set: 1, someString: "string9"))
conditions.append(Condition(set: 2, someString: "string4"))
conditions.append(Condition(set: 3, someString: "string0"))
conditions.append(Condition(set: 1, someString: "string1"))
conditions.append(Condition(set: 2, someString: "string6"))

// sort
let sorted = conditions.sorted { (lhs: Condition, rhs: Condition) -> Bool in
    return (lhs.set) < (rhs.set)
}

// printed sorted conditions
for index in 0...conditions.count-1 {
    println("\(sorted[index].set) - \(sorted[index].someString)")
}
Cyrille

I'm not yet proficient in Swift, but the basic idea for a multiple-criteria sort is:

let sorted = conditions.sorted { (lhs: Condition, rhs: Condition) -> Bool in
    if lhs.set == rhs.set {
        return lhs.someString < rhs.someString
    }
    return (lhs.set) < (rhs.set)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I sort a Tcl array by values?

From Dev

How do you sort an array of structs in swift

From Dev

How do I sort structs in a function

From Dev

How do I sort one array by the corresponding values in another array?

From Dev

How do I turn an array of JSON objects into an array of structs with default values in Go?

From Dev

How do I sort a collection based on values in an array

From Dev

How do I set multiple array values by index in Scala?

From Dev

How do I simplify pushing multiple values into an array in Ruby?

From Dev

How do I store multiple values in an array in Android?

From Dev

How do I simplify pushing multiple values into an array in Ruby?

From Dev

How do I set multiple array values by index in Scala?

From Dev

How do I pass an address of array of structs to a function?

From Dev

How do I declare a dynamic array of structs that contain a Queue member?

From Dev

How do I sort a slice by values of a map

From Dev

How do i sort the values in a dataframe in pandas?

From Dev

How do I sort a multi-dimensional array by time values in PHP?

From Dev

How do I sort an array of arrays containing two values in Objective-C?

From Dev

How do I sort a multi-dimensional array by time values in PHP?

From Dev

How do I sort an array filled with objects, based on two values of the object?

From Dev

How do I sort an array ignoring capitalization?

From Dev

How do I sort an array alphabetically?

From Dev

How do I sort this array by this value?

From Dev

How do I sort a dataframe by an array not in the dataframe

From Dev

Sort array by multiple values in PHP

From Dev

Sort array with multiple values in Javascript

From Dev

Sort Array based on multiple values

From Dev

How to sort an array of structs with std::binary_search or std::sort

From Dev

Sort array of structs by string

From Dev

How Do I Sort an Array of Objects using Insertion Sort?

Related Related

  1. 1

    How do I sort a Tcl array by values?

  2. 2

    How do you sort an array of structs in swift

  3. 3

    How do I sort structs in a function

  4. 4

    How do I sort one array by the corresponding values in another array?

  5. 5

    How do I turn an array of JSON objects into an array of structs with default values in Go?

  6. 6

    How do I sort a collection based on values in an array

  7. 7

    How do I set multiple array values by index in Scala?

  8. 8

    How do I simplify pushing multiple values into an array in Ruby?

  9. 9

    How do I store multiple values in an array in Android?

  10. 10

    How do I simplify pushing multiple values into an array in Ruby?

  11. 11

    How do I set multiple array values by index in Scala?

  12. 12

    How do I pass an address of array of structs to a function?

  13. 13

    How do I declare a dynamic array of structs that contain a Queue member?

  14. 14

    How do I sort a slice by values of a map

  15. 15

    How do i sort the values in a dataframe in pandas?

  16. 16

    How do I sort a multi-dimensional array by time values in PHP?

  17. 17

    How do I sort an array of arrays containing two values in Objective-C?

  18. 18

    How do I sort a multi-dimensional array by time values in PHP?

  19. 19

    How do I sort an array filled with objects, based on two values of the object?

  20. 20

    How do I sort an array ignoring capitalization?

  21. 21

    How do I sort an array alphabetically?

  22. 22

    How do I sort this array by this value?

  23. 23

    How do I sort a dataframe by an array not in the dataframe

  24. 24

    Sort array by multiple values in PHP

  25. 25

    Sort array with multiple values in Javascript

  26. 26

    Sort Array based on multiple values

  27. 27

    How to sort an array of structs with std::binary_search or std::sort

  28. 28

    Sort array of structs by string

  29. 29

    How Do I Sort an Array of Objects using Insertion Sort?

HotTag

Archive