class Article
{
public int IsShow;
public string Updated;
}
class Program
{
static void Main(string[] args)
{
List<Article> articles = new List<Article>();
articles.Add(new Article() { IsShow = 1, Updated = "astring" });
articles.Add(new Article() { IsShow = 0, Updated = "gstring" });
articles.Add(new Article() { IsShow = 1, Updated = "dstring" });
articles.Add(new Article() { IsShow = 0, Updated = "mstring" });
articles.Add(new Article() { IsShow = 1, Updated = "tstring" });
articles.Add(new Article() { IsShow = 0, Updated = "wstring" });
articles.Add(new Article() { IsShow = 1, Updated = "cstring" });
foreach (var ar in articles)
{
Console.WriteLine(string.Format("article {0},{1}",ar.IsShow,ar.Updated));
}
Console.WriteLine("sorted");
SortArticles(articles);
foreach (var ar in articles)
{
Console.WriteLine(string.Format("article {0},{1}", ar.IsShow, ar.Updated));
}
SortArticleCamparer articleIsShow = new SortArticleCamparer("IsShow");
SortArticleCamparer articleUpdate = new SortArticleCamparer("Update");
int s = articles.BinarySearch(new Article() { IsShow = 1, Updated = "bstring" }, articleUpdate);
Console.WriteLine(s);
Console.WriteLine(~s);
Console.ReadLine();
}
protected static List<Article> SortArticles(List<Article> articles)
{
SortArticleCamparer articleIsShow = new SortArticleCamparer("IsShow");
SortArticleCamparer articleUpdate = new SortArticleCamparer("Update");
articles.Sort(articleIsShow);
int count = 0;
foreach (var article in articles)
{
if (article.IsShow == 1)
{
break;
}
count++;
}
if (count != 0 || count != articles.Count)
{
articles.Sort(0, count, articleUpdate);
articles.Sort(count, articles.Count - count, articleUpdate);
}
articles.Reverse();
return articles;
}
public class SortArticleCamparer : IComparer<Article>
{
private string sortKind;
public SortArticleCamparer(string sk)
{
this.sortKind = sk;
}
public int Compare(Article article1, Article article2)
{
int res = 0;
if ((article1 == null) && (article2 == null))
{
return 0;
}
else if ((article1 != null) && (article2 == null))
{
return 1;
}
else if ((article1 == null) && (article2 != null))
{
return -1;
}
if (sortKind == "IsShow")
{
res = article1.IsShow.CompareTo(article2.IsShow);
}
else if (sortKind == "Update")
{
res = article1.Updated.CompareTo(article2.Updated);
}
return res;
}
}
}