منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : ترتيب النوع على أساس المفاتيح
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
Sort Dictionary based on keys
عندى كود مثل هذا

كود :
     Dim myDict As New Dictionary(Of String, String)
       myDict.Add("one", 1)
       myDict.Add("four", 4)
       myDict.Add("two", 2)
       myDict.Add("three", 3)

       Dim sortedDict = (From entry In myDict Order By entry.Value Ascending).ToDictionary(Function(pair) pair.Key, Function(pair) pair.Value)

       For Each entry As KeyValuePair(Of String, String) In sortedDict
           Console.WriteLine(String.Format("{0,10} {1,10}", entry.Key, entry.Value))
       Next

هذا الكود يتم ترتيب items ترتيب تصاعديآ لاكن هل يمكن ترتيب items معينة بمعنى

يتم اضافة one و four مثل هذا بعد ذلك يتم الترتيب مثل هذا
four 
one 
two
three 

five
six
seven


هل يمكن ؟
رتب عادي ثم عليك ان تقوم بعمل Swap و المقصد هنا بكلمة Swap هو ان تنقل 4 مكان 1
لكن لتضع في اعتبارك ان كل من Keys و Values الخاصة ب Generic dictionary هما عبارة عن Read only لذلك يجب ان تقوم بتحويل القيمة الخاصة بهم الي Generic List ثم تقم بعمل Swap لهم اي تنقل قيمة معينة مكان قيمة معينة
(20-10-16, 05:03 AM)silverlight كتب : [ -> ]رتب عادي ثم عليك ان تقوم بعمل Swap و المقصد هنا بكلمة Swap هو ان تنقل 4 مكان 1
لكن لتضع في اعتبارك ان كل من Keys و Values الخاصة ب Generic dictionary هما عبارة عن Read only   لذلك يجب ان تقوم بتحويل القيمة الخاصة بهم الي Generic List ثم تقم بعمل Swap لهم اي تنقل قيمة معينة مكان قيمة معينة

لم افهم قصدك جيدآ هل اذا امكن مثال صغير على العلم انه يتم الاضافة الى list
 Dim list As List(Of String) = New List(Of String)
PHP كود :
       Dim list As List(Of String) = New List(Of StringFrom {"One""Two""Three""Four"}

 
       Dim minIndex As Integer 0
        Dim maxIndex 
As Integer 3
        Dim value 
As String = list(minIndex)
 
       list(minIndex) = list(maxIndex)
 
       list(maxIndex) = value

        
' هذا الجزء من الكود لعرض النتائج فقط في ليست بوكس
        For Each s As String In list
            ListBox1.items.add(s)
        Next 
أسلوب اخر 
PHP كود :
       Dim list As List(Of String) = New List(Of StringFrom {"One""Two""Three""Four"}

 
       Dim item As String = list(3)
 
       list.RemoveAt(3)

 
       Dim l As List(Of String) = New List(Of String)
 
       l.Add(item)

 
       For Each s As String In list
 
           l.Add(s)
 
       Next

        For Each ss 
As String In l
            ListBox1
.Items.Add(ss)
 
       Next 
(20-10-16, 05:26 AM)silverlight كتب : [ -> ]
PHP كود :
       Dim list As List(Of String) = New List(Of StringFrom {"One""Two""Three""Four"}

 
       Dim minIndex As Integer 0
        Dim maxIndex 
As Integer 3
        Dim value 
As String = list(minIndex)
 
       list(minIndex) = list(maxIndex)
 
       list(maxIndex) = value

        
' هذا الجزء من الكود لعرض النتائج فقط في ليست بوكس
        For Each s As String In list
            ListBox1.items.add(s)
        Next 

جزاك الله خير  اسف لم افهم جيدآ بعد


كود :
       Dim myDict As New Dictionary(Of String, String) 'انشاء قاموس
       'اضافة items
       myDict.Add("one", 1)
       myDict.Add("four", 4)
       myDict.Add("three", 3)
       myDict.Add("two", 2)
       myDict.Add("five", 5)
       myDict.Add("seven", 7)
       myDict.Add("six", 6)

       Dim templist As List(Of String) = New List(Of String) 'انشاء list of string

       Dim sortedDict = (From entry In myDict Order By entry.Value Ascending).ToDictionary(Function(pair) pair.Key, Function(pair) pair.Value)
       'للترتيب

       For Each entry As KeyValuePair(Of String, String) In sortedDict ' item loop بعد الترتيب
           templist.Add(entry.Key) 'list of string الاضافة فى
           ListBox1.Items.Add(entry.Key) ' الاضافة فى listbox لاظهار النتائج
       Next
الان النتائج سوف تظهر بالترتيب 

كيف نقل مثلآ five و two فى الاول ليك الترتيب بهذا الشكل

five 
two 
one
three
four
six
seven
ممكن مثلآ نقلهم الى list اخرى ثم اعادة الترتيب على هذه الشكل

لاكن فى هذه الكود


كود :
       Dim list As List(Of String) = New List(Of String) From {"One", "Two", "Three", "Four"}

       Dim minIndex As Integer = 0
       Dim maxIndex As Integer = 3
       Dim value As String = list(minIndex)
       list(minIndex) = list(maxIndex)
       list(maxIndex) = value


       ' هذا الجزء من الكود لعرض النتائج فقط في ليست بوكس
       For Each s As String In list
           ListBox1.Items.Add(s)
       Next
لا يتم الترتيب بهذا الشكل
ما تستخدم sorted Dictionary أسهل لك


PHP كود :
       Dim sd As New SortedDictionary(Of IntegerString'انشاء قاموس
        '
اضافة items
        sd
.Add(1"one")
 
       sd.Add(4"four")
 
       sd.Add(3"three")
 
       sd.Add(2"two")
 
       sd.Add(5"five")
 
       sd.Add(7"seven")
 
       sd.Add(6"six")

 
       Dim five As Integer 5
        Dim fifthItem 
As String GetValue(sdfive)
 
       Dim two As Integer 2
        Dim secondItem 
As String GetValue(sdtwo)

 
       sd.Remove(5)
 
       sd.Remove(2)

 
       Dim dic As New Dictionary(Of IntegerString)
 
       dic.Add(fivefifthItem)
 
       dic.Add(twosecondItem)

 
       For Each item In sd
            dic
.Add(item.Keyitem.Value)
 
       Next

        
' عرض النتائج
        For Each s As String In dic.Values
            ListBox1.Items.Add(s)
        Next 


لا تنسي اضافة الدالة التالية الي الكود
والهدف منها الحصول علي Value بواسطة المفتاح الخاص بها


PHP كود :
Public Function GetValue(sd As SortedDictionary(Of IntegerString), key As Integer) As String
        Dim result 
As String Nothing
        If Not sd
.TryGetValue(keyresultThen
            Return Nothing
        End 
If
 
       Return result
    End 
Function 
(20-10-16, 08:07 AM)silverlight        dic.Add(two, secondItem) كتب : [ -> ]
PHP كود :
       For Each item In sd
            dic
.Add(item.Keyitem.Value)
 
       Next

        
' عرض النتائج
        For Each s As String In dic.Values
            ListBox1.Items.Add(s)
        Next 


لا تنسي اضافة الدالة التالية الي الكود
والهدف منها الحصول علي Value بواسطة المفتاح الخاص بها


PHP كود :
Public Function GetValue(sd As SortedDictionary(Of IntegerString), key As Integer) As String
        Dim result 
As String Nothing
        If Not sd
.TryGetValue(keyresultThen
            Return Nothing
        End 
If
 
       Return result
    End 
Function 

جزاك الله خير