الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سوأل بخصوص ترتيب مصفوفة

بدأه fahad com في 1 يونيو 2012 · 6 رد · 1,180 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلآإم عليكم

أعزائي انآإ عندي مصصفوفتين واحدة لإإسسأمي الطلآإب والثأنية رقمية الـ درجاتهم وأبي ارتبهم وفقآإ للدرجآإت من الاعلى الى الأسسفل مع عرض الإسم +الدرجة بـ ListBox

المششكلة الي وإجهتني انه لو رتبت المصفوفة الرقمية لحالهآإ وتركت النصية فآ راح يلخبط لي اسأمي الطلأب ورجاتهم يعني لو محمد بالاول سجلته نسبة 90 رأح يأخذ نسبة شخص أسمه

أحمد وهأكذأ في بقية الطلآإب هذأ الكود حق المصصفوفة الأولى:

string[] names = new string[20];

string[] GPA = new string[20];

وأعتقد خآصصية array.Sort مرأح تفيدني في هل حإلة لانه يرتب بنآإء على الحروف

تم تعديل هذه المشاركة بواسطة fahad com في 1 يونيو 2012 في 02:50

#2

بامكانك عمل كلاس يحتوي على الاسم والعلامة أو استخدام KeyValuePair class

            string[] names = new string[20];
            names[0] = "Akram";
            names[1] = "Ahmad";
            int[] GPA = new int[20];
            GPA[0] = 90;
            GPA[1] = 95;
            List<KeyValuePair<string, int>> temp = new List<KeyValuePair<string, int>>();
            for (int i = 0; i < 20; i++)
                temp.Add(new KeyValuePair<string, int>(names, GPA));
            temp.Sort((t1, t2) => t2.Value.CompareTo(t1.Value));
            for (int i = 0; i < 20; i++)
            {
                names = temp.Key;
                GPA = temp.Value;
            }

قولوا لا اله الا الله تفلحوا

ومن يبتغي غير الاسلام دينا فلن يقبل منه وهو في الاخرة من الخاسرين- صدق الله العظيم

#3
akramKamal كتب:

بامكانك عمل كلاس يحتوي على الاسم والعلامة أو استخدام KeyValuePair class

            string[] names = new string[20];
            names[0] = "Akram";
            names[1] = "Ahmad";
            int[] GPA = new int[20];
            GPA[0] = 90;
            GPA[1] = 95;
            List<KeyValuePair<string, int>> temp = new List<KeyValuePair<string, int>>();
            for (int i = 0; i < 20; i++)
                temp.Add(new KeyValuePair<string, int>(names, GPA));
            temp.Sort((t1, t2) => t2.Value.CompareTo(t1.Value));
            for (int i = 0; i < 20; i++)
            {
                names = temp.Key;
                GPA = temp.Value;
            }

يعطيك العافيه مآإقصصصرت وزبط معي بسس لو تقدر تششرح لي الكود لآإهنت يعني الـ t1 دلالة على ايش والـ t2 على ايش؟! ومتى حددت أنه الـ sort يكون بألـ int؟

الله يقويك

#4

في البداية هل تعرف شيء عن ال delegate

اذا كان لا فأرجو أن تبحث عن هذا الموضوع لأنه مهم جدا فهم هذا النوع من الاستدعاعات

بالنسبة لسؤالك الثاني,

لاحظ انني استخدمت KeyValuePair<string, int> أي قيمة نصية لKey و قيمة عددية لValue

وعندما قمت بعملية الترتيب استعملت القيمة العددية Value

temp.Sort((t1, t2) => t2.Value.CompareTo(t1.Value));

قولوا لا اله الا الله تفلحوا

ومن يبتغي غير الاسلام دينا فلن يقبل منه وهو في الاخرة من الخاسرين- صدق الله العظيم

#5

يعطيك العافيه مإقصصصرت

#6

This is can be simply done by swapping the names whenever you swap its corresponding gpa’s. So if you have the following two arrays

static string[] _names = { "a", "b", "c", "d" };
static float[] _gpa = { 2.0F, 4.0F, 3.0F, 1.0F };

Where the first is an array of strings which are the students’ names, and the second on is an array of floats which is the students’ gpa’s.

Using one of the simplest sorting algorithms which is the bubble sort, we would have

for (int i = 1; i <= _names.Length; i++)
   for (int j = 0; j < _names.Length - i; j++)
      if (_gpa[j] < _gpa[j + 1])
          swap(j);

where sawp is the following simple method

public static void swap(int j)
      {
        float ftemp = _gpa[j];
        string stemp = _names[j];
        _gpa[j] = _gpa[j + 1];
        _names[j] = _names[j + 1];
        _gpa[j + 1] = ftemp;
        _names[j + 1] = stemp;
      }

I am one of those programmers who believe that C# new techniques in improving the readability of the code are performance killers. The given solution in this post is 28 times faster than the neat and very clear solution that was previously provided by Mr. Akram. You can try Stopwatch to prove that. Please note that we are using one of the slowest sorting algorithms, bubble sort, maybe you need to implement a more powerful algorithm and check the performance after that.

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

#7
  • يمكنك استخدام Dictionary
  • يمكنك استخدام مصفوفة ذات بعدين، حيث تقوم بتخزين رقم الطالب و درجته
  • يمكنك...

..:: رَبِّ زِدْني عِلْمًا ::..

مواضيع مشابهة