J’ai des cours comme:
class SortNode { public Int32 m_valRating = 0; public SortNode(Int32 valRating) { this.m_valRating = valRating; } }
et certaines listes refSortNodeList
:
List refSortNodeList = new List(); Random refRandom = new Random(); for (int i = 0; i < 100; ++i) { refSortNodeList.Add(new SortNode(refRandom.Next(-10, 30))); } foreach (var varSortNode in refSortNodeList) { Console.WriteLine("SortNode rating is {0}", varSortNode.m_valRating); }
Comment sortinger facilement mon refSortNodeList
par le champ m_valRating
? Ou peut-être dois-je utiliser une autre classe List
?
list.Sort((x,y) => x.m_valRating.CompareTo(y.m_valRating));
En place:
refSortNodeList.Sort( (x, y) => x == null ? (y == null ? 0 : -1) : (y == null ? 1 : x.m_valRating.CompareTo(y.m_valRating)) );
Création d’une nouvelle énumération:
var newEnum = refSortNodeList.OrderBy(x => x.m_valRating);
Créer une nouvelle liste:
var newList = refSortNodeList.OrderBy(x => x.m_valRating).ToList();
La mise en place est la plus rapide et la plus efficace en termes de mémoire, mais inutile si vous souhaitez également conserver l’ancienne liste.
La prochaine est plus rapide que la dernière et donne les résultats au fur et à mesure, mais vous devez refaire le sorting pour le réutiliser, auquel cas le troisième est celui à utiliser.
Utilisez Linq order by.
var mySortedList = refSortNodeList.OrderBy(x => x.m_valRating);
Voici un exemple réel où je tire une liste d’une firebase database, mais c’est exactement le même concept.
vendorProducts = (from vp in db.COMPANIES_VND_PRODUCTS join p in db.CT_CT_INV_CLASSES on vp.CLASS_ID equals p.CLASS_ID join m in db.CT_CT_MODALITY_CODES on vp.MODALITY_ID equals m.MODALITY_ID where vp.COMPANY_ID == companyId select new ProductTypeModality { Active = p.ACTIVE.Equals("Y") ? true : false, BioMedImaging = p.BIOMED_IMAGING, Code = p.CLASS_CODE, Description = p.DESCRIPTION, Id = p.CLASS_ID, PricingMargin = p.PRICING_MARGIN, ModalityCode = m.MODALITY_CODE, ModalityId = m.MODALITY_ID, VendorId = companyId }).OrderBy(x => x.Code).ToList();
Implémenter IComparable
C’est facile avec linq:
var newlist = refSortNodeList.sort( n => n.m_valRating );
Vous pouvez utiliser Linq pour les sortings de base:
refSortNodeList.OrderBy(n => n.m_valRating);
Si vous avez besoin d’un sorting plus complexe, vous devrez implémenter IComparable
pour utiliser le sorting intégré.
Essaye ça:
refSortNodeList.Sort(new delgate(SortNode x, SortNode y) { return x.CompareTo(y); } );
List refSortNodeList = new List (); Random refRandom = new Random (); for (int i = 0; i < 100; ++i) { refSortNodeList.Add (new SortNode (refRandom.Next (-10, 30))); } // Use this (Linq) if you're using .NET 3.5 or above. var sortedList = refSortNodeList.OrderBy (node => node.m_valRating); foreach (var varSortNode in sortedList) { Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating); } // Use this otherwise (eg .NET 2.0) refSortNodeList.Sort ( delegate (SortNode n1, SortNode n2) { return n1.m_valRating.CompareTo (n2.m_valRating); } ); foreach (var varSortNode in refSortNodeList) { Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating); }