# Reordering custom arrays

**URL:** <https://forums.swift.org/t/reordering-custom-arrays/56758>\
**Category:** Using Swift\
**Tags:** array, sorting, struct\
**Created:** [April 14, 2022, 7:23pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758 "2022-04-14T19:23:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cj.whitaker](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@cj.whitaker](https://forums.swift.org/u/cj.whitaker)\
**Post date:** [April 14, 2022, 7:23pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758/1 "2022-04-14T19:23:05Z")

</div>

Is there a better way to do this? I want the order in the array to be: Initials -\> Damage -\> Option -\> Condition -\> Unrelated

```swift
struct claimData {
    var isAnInitial = Bool()
    var isDamage = Bool()
    var isUnrelated = Bool()
    var isAnOption = Bool()
    var isACondition = Bool()
}

func constructPrintableArray() -> [claimData] {
        var initialsArray = [claimData]()
        var damageArray = [claimData]()
        var unrelatedArray = [claimData]()
        var optionsArray = [claimData]()
        var conditionsArray = [claimData]()
        
        for claimData in reviewDataController.tableViewReviewData {
            if claimData.isAnInitial {
                initialsArray.append(claimData)
            } else if claimData.isDamage {
                damageArray.append(claimData)
            } else if claimData.isUnrelated {
                unrelatedArray.append(claimData)
            } else if claimData.isAnOption {
                optionsArray.append(claimData)
            } else {
                conditionsArray.append(claimData)
            }
        }
        
        var finalArray = [claimData]()
        finalArray.append(contentsOf: initialsArray)
        finalArray.append(contentsOf: damageArray)
        finalArray.append(contentsOf: optionsArray)
        finalArray.append(contentsOf: conditionsArray)
        finalArray.append(contentsOf: unrelatedArray)
        
        return finalArray
    }

```

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [April 14, 2022, 8:39pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758/2 "2022-04-14T20:39:45Z")

</div>

You could make `claimData` (typically you'd want to capitalize the first letter of a type name btw) conform to Comparable, then just sort the array.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 14, 2022, 8:47pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758/3 "2022-04-14T20:47:56Z")

</div>

> [@David\_Smith](#):
>
> You could make `claimData` (typically you'd want to capitalize the first letter of a type name btw) conform to Comparable, then just sort the array.

Though that would make it O(N\*logN) vs the current O(N), which aspect might be important.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [April 14, 2022, 8:49pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758/4 "2022-04-14T20:49:05Z")

</div>

True. log N on something that fits in a UI is probably ignorably small, but always good to be aware of the possibility.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 14, 2022, 8:59pm UTC](https://forums.swift.org/t/reordering-custom-arrays/56758/5 "2022-04-14T20:59:07Z")

</div>

Somewhat shorter (untested):

```swift
struct ClaimData {
    enum Kind: Int, CaseIterable {
        case initial
        case damage
        case option
        case condition
        case unrelated
    }
    let kind: Kind
    // other fields go here, I assume
}

func constructPrintableArray() -> [ClaimData] {
    var arrays: [[ClaimData]] = .init(repeating: [], count: ClaimData.Kind.allCases.count)
    
    for claimData in reviewDataController.tableViewReviewData {
        arrays[claimData.kind.rawValue].append(claimData)
    }
    
    return arrays.flatMap { $0 }
}

```
