Skip to content

Commit

Permalink
Improve playlist sorting
Browse files Browse the repository at this point in the history
Add secondary and tertiary sorting options to behave more like spotify
when sorting by artist or album.
  • Loading branch information
Roger-Roger-debug committed Feb 9, 2022
1 parent 3bc22a9 commit bbf4ceb
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/model/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl Playlist {
}

pub fn sort(&mut self, key: &SortKey, direction: &SortDirection) {
fn compare_artists(a: Vec<String>, b: Vec<String>) -> Ordering {
let sanitize_artists_name = |x: Vec<String>| -> Vec<String> {
fn compare_artists(a: &Vec<String>, b: &Vec<String>) -> Ordering {
let sanitize_artists_name = |x: &Vec<String>| -> Vec<String> {
x.iter()
.map(|x| {
x.to_lowercase()
Expand All @@ -113,34 +113,31 @@ impl Playlist {
a.cmp(&b)
}

fn compare_album(a: &Track, b: &Track) -> Ordering {
a.album
.as_ref()
.map(|x| x.to_lowercase())
.cmp(&b.album.as_ref().map(|x| x.to_lowercase()))
.then_with(|| a.disc_number.cmp(&b.disc_number))
.then_with(|| a.track_number.cmp(&b.track_number))
}

if let Some(c) = self.tracks.as_mut() {
c.sort_by(|a, b| match (a.track(), b.track()) {
(Some(a), Some(b)) => match (key, direction) {
(SortKey::Title, SortDirection::Ascending) => {
a.title.to_lowercase().cmp(&b.title.to_lowercase())
}
(SortKey::Title, SortDirection::Descending) => {
b.title.to_lowercase().cmp(&a.title.to_lowercase())
(Some(a), Some(b)) => {
let (a, b) = match direction {
&SortDirection::Ascending => (a, b),
&SortDirection::Descending => (b, a),
};
match *key {
SortKey::Title => a.title.to_lowercase().cmp(&b.title.to_lowercase()),
SortKey::Duration => a.duration.cmp(&b.duration),
SortKey::Album => compare_album(&a, &b),
SortKey::Added => a.added_at.cmp(&b.added_at),
SortKey::Artist => compare_artists(&a.artists, &b.artists)
.then_with(|| compare_album(&a, &b)),
}
(SortKey::Duration, SortDirection::Ascending) => a.duration.cmp(&b.duration),
(SortKey::Duration, SortDirection::Descending) => b.duration.cmp(&a.duration),
(SortKey::Album, SortDirection::Ascending) => a
.album
.map(|x| x.to_lowercase())
.cmp(&b.album.map(|x| x.to_lowercase())),
(SortKey::Album, SortDirection::Descending) => b
.album
.map(|x| x.to_lowercase())
.cmp(&a.album.map(|x| x.to_lowercase())),
(SortKey::Added, SortDirection::Ascending) => a.added_at.cmp(&b.added_at),
(SortKey::Added, SortDirection::Descending) => b.added_at.cmp(&a.added_at),
(SortKey::Artist, SortDirection::Ascending) => {
compare_artists(a.artists, b.artists)
}
(SortKey::Artist, SortDirection::Descending) => {
compare_artists(b.artists, a.artists)
}
},
}
_ => std::cmp::Ordering::Equal,
})
}
Expand Down

0 comments on commit bbf4ceb

Please sign in to comment.