Pour votre cas, vous devez créer un nouveau qui peut trier Comparer en lexicograhical order Vous devez avoir votre propre implémentation de l'interface comparateur qui implémente IComparer<string>
. Votre comparateur devrait ressembler à ceci:
public class LexicoComparer : IComparer<string>
{
public int Compare(string x, string y)
{
var firstArgumentSplits = x.Split(' ');
var secondArgumentSplits = y.Split(' ');
if (firstArgumentSplits.Length == 2 && secondArgumentSplits.Length == 2)
{
int firstArgumentInt, secondArgumentInt;
if (firstArgumentSplits[0] == secondArgumentSplits[0])
{
if (int.TryParse(firstArgumentSplits[1], out firstArgumentInt))
{
if (int.TryParse(secondArgumentSplits[1], out secondArgumentInt))
{
return firstArgumentInt == secondArgumentInt ? 0 : firstArgumentInt > secondArgumentInt ? 1 : 0;
}
}
}
}
return String.Compare(x, y);
}
}
Maintenant, vous utilisez plus haut comparateur dans OrderBy
:
ViewBag.AddrBarangay = new SelectList(db.Barangays,
"BarangayId", "BarangayName", "").OrderBy(m => m.Text,
new LexicoComparer());
Pouvez-vous s'il vous plaît laissez-moi savoir, le numéro est avec le texte ou sa colonne séparée? ou vous concaténéz les deux? comprends-tu mon point? – Rajpurohit
@Rajpurohit c'est avec le texte. – KiRa
vous voulez dire "BarangayName" colonne contient "Barangay 1", "Barangay 101" droite? – Rajpurohit