Swift: sort an array in alphabetical order

I am very new to Swift and Objective-C.

I have a table view controller in which I declared a dictionary and an array:

var parts = [:] var partsSectionTitles: NSArray! 

In my viewDidLoad function, I have:

 parts = [ "Part 1" : ["X:1", "X:2", "X:3"], "Part 2" : ["X:1", "X:2"], "Part 3" : ["X:1"] ] var partsSectionTitles = parts.allKeys 

I have already successfully completed this table view controller in Objective-C, and to sort the parts in the header section in alphabetical order, I used:

 partsSectionTitles = [[parts allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

So my question is: how to write the previous Objective-C code in Swift? Thank you in advance for your answers.

UPDATE: I was able to solve the problem using bits and snippets from the answers you provided to you. So thank you! Here is what I have:

I declared the details dictionary as:

 var parts = [String:[String]]() 

which allowed me to provide multiple values โ€‹โ€‹to each key. It was a HUGE help.

Then I managed to create partSectionTitles and sort it:

 partsSectionTitles = [String](parts.keys) partsSectionTitles.sort(){ $0 < $1 } 

This worked as I received no errors.

+6
source share
4 answers

What about a good Quicksort function to order your array of strings? ( Source )

First create an array extension to decompose the given array:

 extension Array { var decompose : (head: T, tail: [T])? { return (count > 0) ? (self[0], Array(self[1..<count])) : nil } } 

Second, create a Quicksort function so you can arrange the expanded array:

 func qsort(input: [String]) -> [String] { if let (pivot, rest) = input.decompose { let lesser = rest.filter { $0 < pivot } let greater = rest.filter { $0 >= pivot } return qsort(lesser) + [pivot] + qsort(greater) } else { return [] } } 

Declare variables with an explicit type (dictionary and array):

 var partsOfNovel = [โ€‹Stringโ€‹ : [String]]() var partsSectionTitles = [String]() 

Fill them out:

 partsOfNovel = [ "Part 1" : ["Chapter X:1", "Chapter X:2", "Chapter X:3"], "Part 2" : ["Chapter X:1", "Chapter X:2"], "Part 3" : ["Chapter X:1"] ] partsSectionTitles = partsOfNovel.allKeys // ["Part 1", "Part 3", "Part 2"] 

And finally, order your array of strings:

 var orderedSectionTitles = qsort(partsSectionTitles) // ["Part 1", "Part 2", "Part 3"] 

I think this is a good solution in pure Swift , hope this helps!

+5
source

This should satisfy your needs. The array contains a String element.

 var partsSectionTitles:[String] = partsOfNovel.allKeys as [String] var sortedNames = partsSectionTitles.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedDescending } 
+10
source
 let array = ["j","d","k","h","m"] func backward(x : String, y: String) -> Bool { return x>y } var reverse = sorted(array,backward) println((reverse)) 

or you can write like:

 var newrev = sorted(array, {$0 < $1}) 

or

 var other = sorted (array , >) 
+4
source

Let's say you have the following Objective-C code with NSDictionary and NSArray :

 NSDictionary *partsOfNovel = @{@"Part 1" : @[@"Chapter X:1", @"Chapter X:2", @"Chapter X:3"], @"Part 4" : @[@"Chapter X:1", @"Chapter X:2"], @"Part 3" : @[@"Chapter X:1"]}; NSArray *partsSectionTitles = [[partsOfNovel allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; NSLog(@"%@", partsSectionTitles); 

With this code, your console will print:

 "Part 1", "Part 3", "Part 4" 

With Swift, you can get Dictionary keys and put them in Array as follows:

 let partsOfNovel = [ "Part 1" : ["Chapter X:1", "Chapter X:2", "Chapter X:3"], "Part 4" : ["Chapter X:1", "Chapter X:2"], "Part 3" : ["Chapter X:1"]] let nonOrderedPartsSectionTitles = partsOfNovel.keys.array 

Then you can apply sorting on this Array and print the result as follows:

 let partsSectionTitles = nonOrderedPartsSectionTitles.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending } println(partsSectionTitles) //[Part 1, Part 3, Part 4] 

But all this can be done using Swift in short form (tested on the Playground):

 let partsOfNovel = [ "Part 1" : ["Chapter X:1", "Chapter X:2", "Chapter X:3"], "Part 4" : ["Chapter X:1", "Chapter X:2"], "Part 3" : ["Chapter X:1"]] let partsSectionTitles = partsOfNovel.keys.array.sorted { $0.0 < $1.0 } println(partsSectionTitles) //[Part 1, Part 3, Part 4] 
+1
source

Source: https://habr.com/ru/post/977907/


All Articles