2012-08-29 13 views
15

क्या कोई मुझे सिखा सकता है कि सी # में क्रम में सूची में आइटम कैसे सम्मिलित करें?क्रम में सूची में आइटम कैसे सम्मिलित करें?

मेरे पास डेटटाइम ऑफसेट ऑब्जेक्ट्स की एक सूची है, और मैं सूची में नए लोगों को सम्मिलित करना चाहता हूं।

List<DateTimeOffset> TimeList = ... 
// determine the order before insert or add the new item 

क्षमा करें, मेरे प्रश्न को अपडेट करने की आवश्यकता है।

List<customizedClass> ItemList = ... 
//customizedClass contains DateTimeOffset object and other strings, int, etc. 

ItemList.Sort(); // this won't work until set data comparison with DateTimeOffset 
ItemList.OrderBy(); // this won't work until set data comparison with DateTimeOffset 

क्या कोई भी कृपया डेटटाइम ऑफसेट को ओडरबी() के पैरामीटर के रूप में डालने में मेरी सहायता कर सकता है?

मैं भी

ItemList = from s in ItemList 
      orderby s.PublishDate descending // .PublishDate is type DateTime 
      select s; 

की कोशिश की हालांकि, यह इस त्रुटि संदेश देता है,

परोक्ष 'System.Collections.Gerneric.List' के लिए कनवर्ट नहीं कर सकता प्रकार 'System.Linq.IOrderedEnumerable'। एक स्पष्ट रूपांतरण मौजूद (यदि आप एक डाली याद कर रहे हैं?)

+4

क्या आप आवश्यकता होने पर या सॉर्टेडलिस्ट का उपयोग कर अपनी सूची को सॉर्ट नहीं कर सकते? –

+0

'सूची 'आदेश दिया गया है' संग्रह। क्या आप * सॉर्ट * करना चाहते हैं? – adatapost

+1

आप किस बारे में "ऑर्डर" के बारे में बात कर रहे हैं? –

उत्तर

5

संशोधित अपने LINQ, अंत में ToList() जोड़ें:

ItemList = (from s in ItemList 
      orderby s.PublishDate descending 
      select s).ToList(); 

वैकल्पिक रूप से बढ़त के मामलों के लिए एक और चर

var sortedList = from s in .... 
1

एक विशिष्ट सूचकांक करने के लिए आइटम सम्मिलित करने के लिए

आप का उपयोग कर सकते हैं:

DateTimeOffset dto; 

// Current time 
dto = DateTimeOffset.Now; 

//This will insert the item at first position 
TimeList.Insert(0,dto); 

//This will insert the item at last position 
TimeList.Add(dto); 

संग्रह आप LINQ का उपयोग कर सकते क्रमबद्ध करने के लिए:

//This will sort the collection in ascending order 
List<DateTimeOffset> SortedCollection=from dt in TimeList select dt order by dt; 
+2

विस्तार का उपयोग क्यों नहीं करें 'ओडर बी()' –

+0

हां, एश बुर्लाज़ेंको जो हम भी कर सकते हैं। मैं linq में बड़े प्रश्न लिखने के लिए प्रयोग किया जाता है। यही कारण है कि मैंने उपर्युक्त प्रश्न लिखा था जो पहला विचार मेरे दिमाग में आया था। लेकिन मैं तुमसे सहमत हूँ। धन्यवाद। – techfun

+0

'सूची ' का कोई अधिभार नहीं है। इसमें एक सूचकांक लेता है। मुझे लगता है कि आपका मतलब है ['सूची इन्सर्ट'] (http://msdn.microsoft.com/en-us/library/sey5k5z4 (v = vs.100) .aspx)। –

0

आप जो सूचकांक चाहते हैं उसे ढूंढने के बाद आप Insert(index,object) का उपयोग कर सकते हैं।

+0

मुझे लिखने के लिए क्रमबद्ध करें – levi

46

अपनी सूची मान लिया जाये कि पहले से ही आरोही क्रम

var index = TimeList.BinarySearch(dateTimeOffset); 
if (index < 0) index = ~index; 
TimeList.Insert(index, dateTimeOffset); 
+3

क्या आप अपना कोड समझा सकते हैं? अगर उन्हें पता नहीं है कि सूची में कैसे डालना है, तो मुझे संदेह है कि वे जान लेंगे कि 'सूचकांक' क्या करता है। –

+0

@AshBurlaczenko, आप सही हैं लेकिन प्रश्न 1 के संदर्भ के बाद प्रश्न का संदर्भ बदल दिया गया है और मैं इसके लिए बहुत आलसी हूं। –

+12

** एमएसडीएन से ** ** रिटर्न वैल्यू सॉर्ट की गई सूची में आइटम की शून्य-आधारित अनुक्रमणिका, यदि आइटम पाई जाती है; अन्यथा, एक नकारात्मक संख्या जो अगले तत्व की अनुक्रमणिका का बिटवाई पूरक है जो वस्तु से बड़ी है या यदि कोई बड़ा तत्व नहीं है, तो गणना के बिटवाई पूरक। –

7

में क्रमबद्ध हो जाता .NET 4 के साथ आप नए SortedSet<T> अन्यथा आप की-वैल्यू संग्रह SortedList के साथ फंस रहे हैं का उपयोग कर सकते हैं।

SortedSet<DateTimeOffset> TimeList = new SortedSet<DateTimeOffset>(); 
// add DateTimeOffsets here, they will be sorted initially 

नोट: SortedSet<T> कक्षा डुप्लिकेट तत्व स्वीकार नहीं करती है। यदि आइटम पहले से ही सेट में है, तो यह विधि झूठी रिटर्न देती है और अपवाद नहीं फेंकता है।

यदि डुप्लीकेट की अनुमति है तो आप List<DateTimeOffset> का उपयोग कर सकते हैं और इसकी Sort विधि का उपयोग कर सकते हैं।

19

एक @L.B.'s answer का थोड़ा सुधार हुआ संस्करण के लिए हल कर सूची में निर्दिष्ट करें:

public static class ListExt 
{ 
    public static void AddSorted<T>(this List<T> @this, T item) where T: IComparable<T> 
    { 
     if (@this.Count == 0) 
     { 
      @this.Add(item); 
      return; 
     } 
     if (@this[@this.Count-1].CompareTo(item) <= 0) 
     { 
      @this.Add(item); 
      return; 
     } 
     if (@this[0].CompareTo(item) >= 0) 
     { 
      @this.Insert(0, item); 
      return; 
     } 
     int index = @this.BinarySearch(item); 
     if (index < 0) 
      index = ~index; 
     @this.Insert(index, item); 
    } 
} 
+2

इस स्निपेट ने मुझे ऐसे मामले में 1000% प्रदर्शन सुधार प्राप्त किया जहां मैं सॉर्टेडसेट <> का उपयोग नहीं कर सका और उसे बार-बार करना था .ॉर्ट() एक सूची। – Nebu

1

बहुत सरल, ली में डेटा जोड़ने के बाद सेंट

list.OrderBy(a => a.ColumnName).ToList(); 
0

मैं @Noseratio's answer लिया और फिर से काम और साथ @ यह here से Jeppe का जवाब संयुक्त एक समारोह है कि और प्रकार है कि IComparable को लागू नहीं करता है (मैं पथ का एक ObservableCollection के लिए यह आवश्यक) संग्रह के लिए काम करता है पाने के लिए।

/// <summary> 
    /// Inserts a new value into a sorted collection. 
    /// </summary> 
    /// <typeparam name="T">The type of collection values, where the type implements IComparable of itself</typeparam> 
    /// <param name="collection">The source collection</param> 
    /// <param name="item">The item being inserted</param> 
    public static void InsertSorted<T>(this Collection<T> collection, T item) where T : IComparable<T> 
    { 
     InsertSorted(collection, item, Comparer<T>.Create((x, y) => x.CompareTo(y))); 
    } 

    /// <summary> 
    /// Inserts a new value into a sorted collection. 
    /// </summary> 
    /// <typeparam name="T">The type of collection values</typeparam> 
    /// <param name="collection">The source collection</param> 
    /// <param name="item">The item being inserted</param> 
    /// <param name="ComparerFunction">An IComparer to comparer T values, e.g. Comparer&lt;T&gt;.Create((x, y) =&gt; (x.Property &lt; y.Property) ? -1 : (x.Property &gt; y.Property) ? 1 : 0)</param> 
    public static void InsertSorted<T>(this Collection<T> collection, T item, IComparer<T> ComparerFunction) 
    { 
     if (collection.Count == 0) 
     { 
     // Simple add 
     collection.Add(item); 
     } 
     else if (ComparerFunction.Compare(item, collection[collection.Count - 1]) >= 0) 
     { 
     // Add to the end as the item being added is greater than the last item by comparison. 
     collection.Add(item); 
     } 
     else if (ComparerFunction.Compare(item, collection[0]) <= 0) 
     { 
     // Add to the front as the item being added is less than the first item by comparison. 
     collection.Insert(0, item); 
     } 
     else 
     { 
     // Otherwise, search for the place to insert. 
     int index = Array.BinarySearch(collection.ToArray(), item, ComparerFunction); 
     if (index < 0) 
     { 
      // The zero-based index of item if item is found; 
      // otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count. 
      index = ~index; 
     } 
     collection.Insert(index, item); 
     } 
    } 
+1

'collection.ToArray() 'एक और संग्रह बनाएगा जो रैखिक खोज i.e.' संग्रह.इंडेक्सऑफ() 'से अधिक महंगा है। –

संबंधित मुद्दे