سؤال

يمكن لأي شخص أن يساعد، لدي مشكلة في القيام بالفرد، اعتقدت أنني قد فرزها ولكن يبدو أن لا تعمل.

لدي قائمة تخزن القيم التالية

8,6,10,11,7

لدي أيضا قائمة أخرى (الملحقات في صفي ولديها Property تسمى CompanySoreID الحالية الفصول في ترتيب المعرف الذي هو Curery 6،7،8،10،11)

وبالتالي، أحتاج إلى فرزها من 6،7،8،10،11 حسب الطلب المستخدم من القائمة البسيطة التي هي 8،6،10،11،7

لدي بلدي المثلقي (انظر أدناه) وأنا أتصل بهذا الشكل - إنه يدخل ولكن هناك خطأ ما لأن القائمة لا تزال لديها كل فصالتي ولكن لا تزال في حدود 6،7،8،10،11

   // accesories is the IList<Accessories> (hence why i am use ToList)
   // and sortOrder is the simple int list list<int>
   accesories.ToList().Sort(new ItemTpComparer(sortOrder));  

class ItemTpComparer : IComparer<Accessories>
{
    private IList<int> otherList;

    public ItemTpComparer(IList<int> otherList)
    {
        this.otherList = otherList;
    }

    #region IComparer<Accessories> Members

    public int Compare(Accessories x, Accessories y)
    {

        if (otherList.IndexOf(x.AccessoryId) > otherList.IndexOf(y.AccessoryId))
            return 1;

        else if (otherList.IndexOf(x.AccessoryId) < otherList.IndexOf(y.AccessoryId))
            return -1;
        else
            return 0;

        // tried below also didn't work
        //return otherList.IndexOf(x.AccessoryId) - otherList.IndexOf(y.AccessoryId);
هل كانت مفيدة؟

المحلول

المقارنة صحيحة (حتى إصدار سطر واحد التعليق). المشكلة هي ToList() يخلق جديد List تحتوي على نسخة من العناصر في IEnumerable<T> كائن في الأساس، تقوم بإنشاء قائمة جديدة، فرزها ورميها بعيدا.

var sortedList = accesories.ToList();
sortedList.Sort(new ItemTpComparer(sortOrder)); 

الذي أقترح استبداله ب:

var sortedList = accessories.OrderBy(sortOrder.IndexOf).ToList();

بهذه الطريقة، لن يكون هناك تطبيق مقارنة ضروريا. يمكنك أيضا فرز في الترتيب التنازلي بسهولة:

var sortedList = accessories.OrderByDescending(sortOrder.IndexOf).ToList();

إذا كان الكائن هو حقا List<Accessories>, ، يمكنك أيضا فرزها في مكانها:

((List<Accessories>)accessories).Sort(new ItemTpComparer(sortOrder));

نصائح أخرى

أظهر لك مهراد لماذا لم يتم فرز القائمة. أريد أن أعالج أداء المقارنة، وأيضا مشكلة فرز أقل من العناصر التي تم فرزها.

باستخدام IndexOf في قائمة لتحديد موقع الفهرس غير فعال للغاية. يجب أن أحلخلص من خلال العناصر الموجودة في القائمة للعثور على اليمين. استخدم القاموس كوحدا بدلا من ذلك، بهذه الطريقة حلقة فقط من خلال العناصر مرة واحدة:

class ItemTpComparer : IComparer<Accessories> {

   private Dictionary<int, int> index;

   public ItemTpComparer(IList<int> otherList) {
      index = new Dictionary<int, int>();
      for (int i = 0; i < otherList.Count; i++) {
         index.Add(otherList[i], i);
      }
   }

   public int Compare(Accessories x, Accessories y) {
      return index[x.AccessoryId].CompareTo(index[y.AccessoryId]);
   }

}

إذا كنت ترغب في السماح لقائمة القيمة بالفرز لتكون أقصر من قائمة العناصر للفرز، فأنت تحقق مما إذا كانت القيمة موجودة في القاموس:

   public int Compare(Accessories x, Accessories y) {
      int xIndex, yIndex;
      if (!index.TryGetValue(x.AccessoryId, out xIndex)) xIndex = int.MaxValue;
      if (!index.TryGetValue(y.AccessoryId, out yIndex)) yIndex = int.MaxValue;
      return xIndex.CompareTo(yIndex);
   }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top