好久没写东西了,发一个泛型的搜索,排序的库

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 
  7 namespace xxWare.Common
  8 {
  9     public static class GenericExtension
 10     {
 11         #region List<T> search count
 12         public static int CountAll<T>(this List<T> _list, T _searchobj)
 13         {
 14             return (from t in _list where t.Equals(_searchobj) select t).Count();
 15         }
 16 
 17         //Must be Sort before use BinarySearchCountAll
 18         public static int BinarySearchCountAll<T>(this List<T> _list, T _searchobj)
 19         {
 20             int center = _list.BinarySearch(_searchobj);
 21 
 22             if (center >= 0)
 23             {
 24                 int left = center;
 25 
 26                 while (left > 0 && _list[left - 1].Equals(_searchobj))
 27                 {
 28                     left -= 1;
 29                 }
 30 
 31                 int right = center;
 32                 while (right < (_list.Count - 1) && _list[right + 1].Equals(_searchobj))
 33                 {
 34                     right += 1;
 35                 }
 36 
 37                 return (right - left) + 1;
 38             }
 39             else
 40             {
 41                 return center;
 42             }
 43         }
 44 
 45         public static List<T> FindAll<T>(this List<T> _list, T _searchobj)
 46         {
 47             List<T> _founditem = new List<T>();
 48 
 49             foreach (var v in _list)
 50             {
 51                 if (v.Equals(_searchobj))
 52                 {
 53                     _founditem.Add(v);
 54                 }
 55             }
 56 
 57             return _founditem;
 58         }
 59 
 60         //Must be Sort before use BinarySearchAll
 61         public static List<T> BinarySearchAll<T>(this List<T> _list, T _searchobj)
 62         {
 63             List<T> _retobj = new List<T>();
 64 
 65             int center = _list.BinarySearch(_searchobj);
 66 
 67             if (center >= 0)
 68             {
 69                 _retobj.Add(_list[center]);
 70 
 71                 int left = center;
 72 
 73                 while (left > 0 && _list[left - 1].Equals(_searchobj))
 74                 {
 75                     left -= 1;
 76                     _retobj.Add(_list[left]);
 77                 }
 78 
 79                 int right = center;
 80                 while (right < (_list.Count - 1) && _list[right + 1].Equals(_searchobj))
 81                 {
 82                     right += 1;
 83                     _retobj.Add(_list[right]);
 84                 }
 85 
 86             }
 87 
 88             return _retobj;
 89 
 90         }
 91 
 92         public static List<string> SearchStringAll(this List<string> _list, string _searchstr)
 93         {
 94             var v = from q in _list
 95                     where q.Contains(_searchstr)
 96                     select q;
 97             return v.ToList<string>();
 98         }
 99 
100         #endregion
101 
102         #region IList<> Sort
103         /// <summary>
104         /// Sample:
105         /// IList<string> iList = new [] { "Carlton", "Alison", "Bob", "Eric", "David" }; 
106         /// // Sort in-place, by string length 
107         /// iList.Sort((s1, s2) => s1.Length.CompareTo(s2.Length)); 
108         /// // Or use OrderBy() 
109         /// IEnumerable<string> ordered = iList.OrderBy((s1, s2) => s1.Length.CompareTo(s2.Length)); 
110         /// </summary>
111         /// <typeparam name="T"></typeparam>
112         /// <param name="list"></param>
113         /// <param name="comparison"></param>
114         public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
115         {
116             ArrayList.Adapter((IList)list).Sort(new ComparisonComparer<T>(comparison));
117         }
118         public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, Comparison<T> comparison)
119         {
120             return list.OrderBy(t => t, new ComparisonComparer<T>(comparison));
121         }
122 
123         public class ComparisonComparer<T> : IComparer<T>, IComparer
124         {
125             private readonly Comparison<T> _comparison;
126             public ComparisonComparer(Comparison<T> comparison) { _comparison = comparison; }
127             public int Compare(T x, T y) { return _comparison(x, y); }
128             public int Compare(object o1, object o2) { return _comparison((T)o1, (T)o2); }
129         }
130         #endregion
131 
132     }
133 }

 

posted @ 2013-01-22 02:59  jdxx  阅读(552)  评论(0编辑  收藏  举报