-2

I have a list of vinyls sorted like this:

Touch Me
49ERS
Everything
49ERS
Touch Me
49ERS
How Longer
49ERS

and I am using this code:

self.vinyls.sort(by: { (vinyl1, vinyl2) -> Bool in
    return vinyl1.artist < vinyl2.artist
})

But I wanted to sort it like this:

Touch Me
49ERS
Touch Me
49ERS
Everything
49ERS
How Longer
49ERS

How would I do that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Eduardo
  • 3
  • 3
  • 1
    Could you clarify how exactly you want it to be sorted? The second list isn't in alphabetical order so I'm not sure what your expected result would be in the general case. – John Montgomery Apr 18 '19 at 17:52
  • What results are you currently getting? What is actually in the array? – rmaddy Apr 18 '19 at 17:52
  • They are all sorted by the artist name but I want the same title to be close to each other like I wrote above. – Eduardo Apr 18 '19 at 17:53
  • What is `vinyl`. What is the property `artist`. How is it related to your question? What kind of sort do you want to do? – Rakesha Shastri Apr 18 '19 at 17:53
  • @Eduardo Let me see if I'm understanding this right: sort by artist, then if there are multiple instances of the same song, group them next to the first instance, otherwise keep the order the same? – John Montgomery Apr 18 '19 at 17:55
  • I have a collection of vinyl and I am saving their names to a database and I want to sort them out in artist order but I want to put the ones with same song name close to each other sorted by artist name. – Eduardo Apr 18 '19 at 17:58
  • Exactly John Montgomery thank you – Eduardo Apr 18 '19 at 17:59
  • @Eduardo But do you need to keep the original song order aside from that? Because if you just sort the songs alphabetically it would be much easier. – John Montgomery Apr 18 '19 at 17:59
  • Yes but I want to sort them by Artists and once they are sorted by Artist I want the titles to be close if there are more than one with the same name.. – Eduardo Apr 18 '19 at 18:01
  • @Eduardo Yes, I get that. I'm saying, is the original order of the non-duplicate songs important? For instance, rpecka's answer will put the duplicates next to each other, but it will change the song order to Everything, How Longer, Touch Me, Touch Me. Is that ok, or does it have to be the exact same order you've shown? – John Montgomery Apr 18 '19 at 18:03
  • It is ok as long they are sorted by artist and have the song titles close to each other it is fine.Thank you very much! – Eduardo Apr 18 '19 at 18:12

1 Answers1

1

You can add more code to your comparison to make it check to see if the artist is equal and then check the track.

if vinyl1.artist < vinyl2.artist {
     return true
} else if vinyl1.artist == vinyl2.artist {
     return vinyl1.track < vinyl2.track
} else {
     return false
}
rpecka
  • 1,168
  • 5
  • 17
  • You could simplify this by removing the first condition and changing the else to `return vinyl1.artist < vinyl2.artist`. But it's not clear if this is what the OP wants since his list isn't sorted alphabetically. (also it's "vinyl," not "vynil") – John Montgomery Apr 18 '19 at 18:00
  • You're definitely right about the condition efficiency and I would definitely recommend using your code in practice but I'm going to leave that part as is so that people understand the logic. Thanks for catching the spelling, I'll fix it. – rpecka Apr 18 '19 at 18:13