public static int Compare(int progid1, int progid2)
{
lock (sortCacheLock)
{
if (sortCache == null || !sortCache.ContainsKey(progid1) || !sortCache.ContainsKey(progid2))
{
// The sort cache is either empty or missing one of the values that are required, so recreate it
sortCache = new Dictionary<int, int>();
int sort = 0;
string orderBy = null;
switch (sortBy)
{
case FavouriteCols.ProgrammeName:
orderBy = "programmes.name" + (sortAsc ? string.Empty : " desc");
break;
case FavouriteCols.Provider:
orderBy = "programmes.pluginid" + (sortAsc ? " desc" : string.Empty);
break;
default:
throw new InvalidDataException("Invalid column: " + sortBy.ToString());
}
using (SQLiteCommand command = new SQLiteCommand("select favourites.progid from favourites, programmes where programmes.progid=favourites.progid order by " + orderBy, FetchDbConn()))
{
using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader()))
{
int progidOrdinal = reader.GetOrdinal("progid");
while (reader.Read())
{
sortCache.Add(reader.GetInt32(progidOrdinal), sort);
sort += 1;
}
}
}
}
return sortCache[progid1] - sortCache[progid2];
}
}