المشاركات : 193
المواضيع 106
الإنتساب : Jul 2016
السمعة :
1
الشكر: 161
تم شكره 32 مرات في 27 مشاركات
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
هل يمكن ؟
المشاركات : 1,486
المواضيع 101
الإنتساب : Sep 2012
السمعة :
127
الشكر: 71
تم شكره 6155 مرات في 1035 مشاركات
رتب عادي ثم عليك ان تقوم بعمل Swap و المقصد هنا بكلمة Swap هو ان تنقل 4 مكان 1
لكن لتضع في اعتبارك ان كل من Keys و Values الخاصة ب Generic dictionary هما عبارة عن Read only لذلك يجب ان تقوم بتحويل القيمة الخاصة بهم الي Generic List ثم تقم بعمل Swap لهم اي تنقل قيمة معينة مكان قيمة معينة
المشاركات : 193
المواضيع 106
الإنتساب : Jul 2016
السمعة :
1
الشكر: 161
تم شكره 32 مرات في 27 مشاركات
20-10-16, 05:15 AM
(آخر تعديل لهذه المشاركة : 20-10-16, 05:23 AM {2} بواسطة Mohamed20.)
(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)
المشاركات : 1,486
المواضيع 101
الإنتساب : Sep 2012
السمعة :
127
الشكر: 71
تم شكره 6155 مرات في 1035 مشاركات
20-10-16, 05:26 AM
(آخر تعديل لهذه المشاركة : 20-10-16, 05:57 AM {2} بواسطة silverlight.)
PHP كود :
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
أسلوب اخر
PHP كود :
Dim list As List(Of String) = New List(Of String) From {"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
المشاركات : 193
المواضيع 106
الإنتساب : Jul 2016
السمعة :
1
الشكر: 161
تم شكره 32 مرات في 27 مشاركات
20-10-16, 06:06 AM
(آخر تعديل لهذه المشاركة : 20-10-16, 06:07 AM {2} بواسطة Mohamed20.)
(20-10-16, 05:26 AM)silverlight كتب : PHP كود :
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
جزاك الله خير اسف لم افهم جيدآ بعد
كود :
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
لا يتم الترتيب بهذا الشكل
المشاركات : 1,486
المواضيع 101
الإنتساب : Sep 2012
السمعة :
127
الشكر: 71
تم شكره 6155 مرات في 1035 مشاركات
20-10-16, 08:07 AM
(آخر تعديل لهذه المشاركة : 20-10-16, 08:09 AM {2} بواسطة silverlight.)
ما تستخدم sorted Dictionary أسهل لك
PHP كود :
Dim sd As New SortedDictionary(Of Integer, String) 'انشاء قاموس 'اضافة 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(sd, five) Dim two As Integer = 2 Dim secondItem As String = GetValue(sd, two)
sd.Remove(5) sd.Remove(2)
Dim dic As New Dictionary(Of Integer, String) dic.Add(five, fifthItem) dic.Add(two, secondItem)
For Each item In sd dic.Add(item.Key, item.Value) Next
' عرض النتائج For Each s As String In dic.Values ListBox1.Items.Add(s) Next
لا تنسي اضافة الدالة التالية الي الكود
والهدف منها الحصول علي Value بواسطة المفتاح الخاص بها
PHP كود :
Public Function GetValue(sd As SortedDictionary(Of Integer, String), key As Integer) As String Dim result As String = Nothing If Not sd.TryGetValue(key, result) Then Return Nothing End If Return result End Function
المشاركات : 193
المواضيع 106
الإنتساب : Jul 2016
السمعة :
1
الشكر: 161
تم شكره 32 مرات في 27 مشاركات
20-10-16, 12:17 PM
(آخر تعديل لهذه المشاركة : 20-10-16, 02:52 PM {2} بواسطة Mohamed20.)
(20-10-16, 08:07 AM)silverlight dic.Add(two, secondItem) كتب : PHP كود :
For Each item In sd dic.Add(item.Key, item.Value) Next
' عرض النتائج For Each s As String In dic.Values ListBox1.Items.Add(s) Next
لا تنسي اضافة الدالة التالية الي الكود
والهدف منها الحصول علي Value بواسطة المفتاح الخاص بها
PHP كود :
Public Function GetValue(sd As SortedDictionary(Of Integer, String), key As Integer) As String Dim result As String = Nothing If Not sd.TryGetValue(key, result) Then Return Nothing End If Return result End Function
جزاك الله خير
|