-5

Regarding Marked As Duplicate - I don't think this is a duplicate because I'm not comparing an array to another existing array to get an order. Please see the answer I submitted to my own question for what I wrote to accomplish what I needed. This is an old question that I tried to improve with a better title.

I looked up sorting of arrays and I see how to reorder by alphabet or ascending dates or numbers, but I want to set a specific arbitrary order.

I have a group of .txt files in Bundle titled in a foreign non latin language (so I can't do alphabetical I think) and I load the titles into a tableview.

func loadStories() {
    stories = Bundle.main.urls(forResourcesWithExtension: "txt", subdirectory: nil)!
}

so I have var stories: [URL] = [] and there are about 20 stories. How do I set the order into a specific order that has no alphabet/numerical logic? I want to just list the names in my preferred way of listing.

So I have a list of txt tiles that randomly populates my tableView - Story Ba, Story Bc, Story Ac, Story Cb, Story Ca, Story Aa, Story Bb.

I want it to populate the tableview in this specific order: Story Ba, Story Bb, Story Ac, Story Ca, Story Cb, Story Aa, Story Bc. There is no obvious logic to this order like alphabetical/numerical/etc so I can't use .sorted() or filter.

I'm looking to set the list to an arbitrary order that I can hardcode - how can I do this?

bobcat
  • 177
  • 1
  • 12
  • You need to update your question with at least a few of the exact URLs you wish to sort and clearly show the desired order (and explain the logic you used). Also note that you don't need a Latin alphabet to sort text. – rmaddy Aug 20 '18 at 00:36
  • If you want a random order then that is not a sort. Just randomize the array. – rmaddy Aug 20 '18 at 02:05
  • Sorry but if you can't describe the logic to how you want the URLs ordered then no one can help you do it. You say there is no logic but you also say it isn't random. That makes no sense. – rmaddy Aug 20 '18 at 03:12

2 Answers2

1

Use Swift's block based sorting, you can write the comparison in any way you want to. Compare $0 with $1 using if statements or any algorithm of your choosing :

sortedStories = stories.sorted( by: {

   // Your preferred way goes here, I just put alphabetical
   if $0.lastPathComponent < $1.lastPathComponent { return true }

   return false
})

You will need to hard code the order of the story files

let order : [String] = [ "story7.txt", "story3.txt", "story1.txt", "story4.txt" ]

And then access these using the indexPath.row in cellForRowAtIndexPath.

Brett
  • 1,647
  • 16
  • 34
  • @bhealth do you mean that they have to be in the order of the file ? Then just store them in an array, an array keeps order. Then in cellForRow you index the array with indexPath.row. If they are not in the correct order in the file, can you make them in the correct order in the file ? Otherwise you will need to hard code the order in your code, and this negates the need for the txt file. – Brett Aug 20 '18 at 04:35
  • Oh I think I see now. There are multiple txt files ? – Brett Aug 20 '18 at 04:41
  • Pretty sure your only option is to either hardcode the order in your code or have another txt file with the order in that. I edited my answer above. – Brett Aug 20 '18 at 05:33
0

In case anyone is looking to do a similar ordering, here is what I ended up doing:

  1. Set the desired arbitrary order for stories

    let storyNames = ["Story Ba", "Story Bb", "Story Ac", "Story Ca", "Story Cb", "Story Aa", "Story Bc"]
    
  2. Use .compactMap to get the stories in that order

    let stories = storyNames.compactMap {
    storyName in
    return Bundle.main.url(forResource: storyName, withExtension: "txt")
    }
    
bobcat
  • 177
  • 1
  • 12
  • @Brett Thank you for your time, but as I realized a few days after accepting your answer, I wasn't able to get my specific order. I had to hardcode the order and then when I learned about .maps and compact maps I realized that it would help me load the stories in the hard coded order. It's not poor form. It's called learning and improving my solutions. – bobcat Oct 03 '18 at 04:06
  • Also not entirely sure why I'm being downvoted. Am I not allowed to updated my accepted answer? I commonly see people answer their own questions as time goes by to show how they successfully solved their problem. An explanation would go much further than downvotes. Or is this an issue because I shouldn't deselect an accepted answer? – bobcat Oct 03 '18 at 04:17
  • 1
    Your answer is based upon the answer I gave you, which you accepted. I told you that you would need to hard code the order (see my answer). Personally I still think it is poor form to take that answer and repost it and then accept it. Doing this means people like myself will be less likely to help you in the future. – Brett Oct 03 '18 at 07:36
  • @Brett Understood and I apologize. I'm still a beginner and I was trying to improve my past questions as I learn new things from multiple sources. Clearly I've ran up against some norms on this site judging from the downvotes. I did not intend to insult the time spent on your answer posted and I am appreciative of it informing my final solution. I hope you won't take this experience to heart in your future contributions to people like myself. – bobcat Oct 03 '18 at 20:30
  • Appreciate you changing it back. All good. – Brett Oct 08 '18 at 01:01