logo
down
shadow

How can I auto-sort an array in Swift by property type?


How can I auto-sort an array in Swift by property type?

By : user2955592
Date : November 22 2020, 10:40 AM
wish help you to fix your issue You have a recursion problem. Every time you touch arrayOfSections, it’ll call configureSections. That includes the changes configureSections is making to arrayOfSections, such as sorting or removing empty sections. You might just about get away with it with the removing of empty sections (because after the removal, the subsequent call doesn’t remove anything, so won’t alter the array and re-call the function), but sorting it pushed things over the edge.
You would probably be better off with a private array, and then a computed property that provided access to it, like this:
code :
struct QuoteLibrary {
    private var _arrayOfSections: [Section] = []

    var title: String
    var arrayOfSectionTitles: [String] = []

    var arrayOfSections: [Section] {
        get { return _arrayOfSections }
        set(newArray) {
            _arrayOfSections = newArray.filter { !$0.arrayOfQuotes.isEmpty }
            _arrayOfSections.sort { $0.title > $1.title }
            arrayOfSectionTitles = _arrayOfSections.map { $0.title }
        }
    }

    init(title: String) { self.title = title }
}


Share : facebook icon twitter icon
Swift how to sort array of custom objects by property value

Swift how to sort array of custom objects by property value


By : jj606
Date : March 29 2020, 07:55 AM
Hope that helps First, declare your Array as a typed array so that you can call methods when you iterate:
code :
var images : [imageFile] = []
images.sorted({ $0.fileID > $1.fileID })
images.sorted(by: { $0.fileID > $1.fileID })
Sort array in Swift using a variable property?

Sort array in Swift using a variable property?


By : vishwajeet
Date : March 29 2020, 07:55 AM
it should still fix some issue If all the properties that are used for sorting have the same type, you could use the built-in key-value access. Consider the following class A with two properties a and b both of which are strings.
code :
class A: NSObject, Printable {
    var a: String
    var b: String

    init(a: String, b: String) {
        self.a = a
        self.b = b
    }

    override var description: String {
        return "{ a: \(self.a), b: \(self.b) }"
    }
}
var tableContents = [A(a: "1", b: "2"), A(a: "2", b: "1")]
tableContents.sort {
    let key = "a"
    let oneValue = $0.valueForKey(key) as! String
    let otherValue = $1.valueForKey(key) as! String
    return oneValue.localizedCaseInsensitiveCompare(otherValue) == NSComparisonResult.OrderedAscending
}

println(tableContents) // [{ a: 1, b: 2 }, { a: 2, b: 1 }]

tableContents.sort {
    let key = "b"
    let oneValue = $0.valueForKey(key) as! String
    let otherValue = $1.valueForKey(key) as! String
    return oneValue.localizedCaseInsensitiveCompare(otherValue) == NSComparisonResult.OrderedAscending
}

println(tableContents) // [{ a: 2, b: 1 }, { a: 1, b: 2 }]
Using an array proxy to auto sort objects by property when item is pushed in

Using an array proxy to auto sort objects by property when item is pushed in


By : Judita Banaitytė
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Problem is underscore sortBy makes a new copy of the array, but you want the original one modified. A simple vanilla javascript sort would fix it:
code :
target.sort(function(a,b){return a.id - b.id})
Swift 4.2 Sort array using a selected property and sort order

Swift 4.2 Sort array using a selected property and sort order


By : Awat Qader
Date : March 29 2020, 07:55 AM
With these it helps I'm trying to sort an array of custom struct in swift 4.2. I want to sort it out in such a way that , To sort the array in place:
code :
myArray.sort { item1, item2 in 
   if item1.isSelected == item2.isSelected {
      return item1.sortOrder < item2.sortOrder 
   }
   return item1.isSelected && !item2.isSelected 
}
let sortedArray = myArray.sorted { item1, item2 in 
   if item1.isSelected == item2.isSelected {
      return item1.sortOrder < item2.sortOrder 
   }
   return item1.isSelected && !item2.isSelected 
}
Array how to sort by mutiple property by Swift?

Array how to sort by mutiple property by Swift?


By : user2835677
Date : March 29 2020, 07:55 AM
Does that help I want to sort array data by number value like "90 > 80 > 70 ...> 10". , Sort the elements by the sum of 3 numbers
code :
array = array.sorted { (item, nextItem) -> Bool in
    let itemSum = item.number1 + item.number2 + item.number3
    let nextItemSum = nextItem.number1 + nextItem.number2 + nextItem.number3
    return itemSum > nextItemSum
}
Related Posts Related Posts :
  • Kotlin: Can I access a nested array from inside another array?
  • minimum complexity to find if a number is present n/2 times or not in a sorted array
  • Powershell: Comparing multiple values in an array
  • Add Elements to nullable Vector
  • How to get elements larger than x in a given range?
  • How to call Array-valued Functions in fortran?
  • How do I create and pass string array to a sub in Excel VBA?
  • Trying to test collisions with objects in array
  • Sort an array of arrays in Erlang
  • push into deeply nested array
  • Encapsulate strings in an array in PowerShell
  • Send Specific variables through socket.io to client with socket.send
  • Max within groups in Matlab
  • when preparing for segue array contains no data (using swift)
  • How do you access the attributes of a class instance from inside an array? (Swift)
  • Array.Find and IndexOf for multiple elements that are exactly the same object
  • How to find array items that occur at least K times
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org