-
Notifications
You must be signed in to change notification settings - Fork 4
/
SortingHelper.cs
70 lines (58 loc) · 2.04 KB
/
SortingHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using SoftdocMusicPlayer.Core.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SoftdocMusicPlayer.Helpers
{
public static class SortingHelper
{
//public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector)
//{
// var sortedSource = source.OrderBy(keySelector).ToList();
// for (var i = 0; i < sortedSource.Count; i++)
// {
// var itemToSort = sortedSource[i];
// // If the item is already at the right position, leave it and continue.
// if (source.IndexOf(itemToSort) == i)
// {
// continue;
// }
// source.Remove(itemToSort);
// source.Insert(i, itemToSort);
// }
//}
public static void Sort<T>(this ObservableCollection<T> collection, Comparison<T> comparison)
{
var sortableList = new List<T>(collection);
sortableList.Sort(comparison);
for (int i = 0; i < sortableList.Count; i++)
{
collection.Move(collection.IndexOf(sortableList[i]), i);
}
}
public static int SortByName(SongModel x, SongModel y)
{
return x.Title.CompareTo(y.Title);
}
public static int SortByAlbum(SongModel x, SongModel y)
{
return x.Album.CompareTo(y.Album);
}
public static int SortByArtist(SongModel x, SongModel y)
{
return x.Artist.CompareTo(y.Artist);
}
public static int SortByAlbumArtist(SongModel x, SongModel y)
{
return x.AlbumArtist.CompareTo(y.AlbumArtist);
}
public static int SortByYear(SongModel x, SongModel y)
{
return x.Year.CompareTo(y.Year);
}
public static int DescendingSortByYear(SongModel x, SongModel y)
{
return y.Year.CompareTo(x.Year);
}
}
}