Click here to Skip to main content
15,500,453 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello guys,

I would like to sort months as string but I get them from the database as integer and I would like to sort them by list classes.

I need someone's help.

My Codes are as below:

C#
public int CompareTo(List_Data data)
     {
         return (this.Month - data.Month);
     }


 public class List_Dondur
 {

     List_Data data;

     private List<List_Data> _list_Kiraci_Bilgi_Ay = new List<List_Data>();

     public List<List_Data> List_Kiraci_Bilgi_Ay
     {
         get { return _list_Kiraci_Bilgi_Ay; }
         set { _list_Kiraci_Bilgi_Ay = value; }
     }

     public List_Dondur()
     {
         List_Kiraci_Bilgi_Ay_1();

         Sirala();
     }

     private void Sortit()
     {
         this.List_Kiraci_Bilgi_Ay.Sort();
     }

     public void List_Kiraci_Bilgi_Ay_1()
     {
         string daire_kiraci = "SELECT DRNO,CRKOD,OT_ID FROM DAIRE_KIRACI";
         ArrayList ls_drno = Baglanti.Komut.IntegerKoleksiyonEkle(daire_kiraci, 0);
         ArrayList ls_crkod = Baglanti.Komut.MetinKoleksiyonEkle(daire_kiraci, 1);
         ArrayList ls_otid = Baglanti.Komut.IntegerKoleksiyonEkle(daire_kiraci, 2);
         //List<List_Data> liste = new List<List_Data>();
         string[] dz_ay = Enum.GetNames(typeof(Months));

         for (int i = 0; i < ls_drno.Count; )
         {
             string crkod = ls_crkod[i].ToString();

             int drno = Convert.ToInt32(ls_drno[i]);

             string drad = Baglanti.Komut.MetinselVeriOku("SELECT DRAD FROM DAIRELER WHERE DRNO='" + drno + "'", 0);
             string adi = Baglanti.Komut.MetinselVeriOku("SELECT ADI FROM CARILER WHERE CRKOD='" + crkod + "'", 0);
             string soyadi = Baglanti.Komut.MetinselVeriOku("SELECT SOYADI FROM CARILER WHERE CRKOD='" + crkod + "'", 0);
             string _array = "SELECT  AY,IS_TUR_ID, SUM(BORC-ALACAK) FROM CRHAREKET WHERE CRKOD='" + crkod + "'  GROUP BY AY,IS_TUR_ID";

             ArrayList ls_ay = Baglanti.Komut.IntegerKoleksiyonEkle(_array, 0);
             ArrayList ls_tur = Baglanti.Komut.IntegerKoleksiyonEkle(_array, 1);
             ArrayList ls_tutar = Baglanti.Komut.DecimalKoleksiyonEkle(_array, 2);

             int krdurum = Convert.ToInt32(ls_otid[i]);
             decimal kira = 0;
             decimal deposit = 0;
             decimal elektrik = 0;
             decimal su = 0;
             string blok = Baglanti.Komut.MetinselVeriOku("SELECT BLADI FROM BLOKLAR B, DAIRELER D WHERE B.BLNO=D.BLNO AND D.DRNO='" + drno + "'", 0);
             for (int k = 0; k < ls_ay.Count; )
             {
                 kira = deposit = elektrik = su = 0;
                 int ay = Convert.ToInt32(ls_ay[k]);
                 string _ay = dz_ay[ay].ToString();
                 int turid = Convert.ToInt32(ls_tur[k]);
                 decimal miktar = Convert.ToDecimal(ls_tutar[k]);
                 if ((int)Islem_Id.DEPOSİT == turid)
                     deposit = miktar;
                 else if ((int)Islem_Id.ELEKTRİK == turid)
                     elektrik = miktar;
                 else if ((int)Islem_Id.KİRA == turid)
                     kira = miktar;
                 else if ((int)Islem_Id.SU == turid)
                 {
                     su = miktar;
                 }
                this.List_Kiraci_Bilgi_Ay.Add(veri=new List_Data(drno, drad, crkod, adi, soyadi, kira, deposit, elektrik, su, blok, krdurum, _month, month));
                 k++;
             }



             i++;
         }
         veri.CompareTo(data);



     }

     decimal Deger_Dondur(string crkod, int is_turid)
     {
         string sorgu = "SELECT SUM(BORC-ALACAK) FROM CRHAREKET WHERE CRKOD='" + crkod + "' AND IS_TUR_ID='" + ıs_turid + "'";
         string sayim = "SELECT COUNT(*) FROM CRHAREKET WHERE CRKOD='" + crkod + "' AND IS_TUR_ID='" + ıs_turid + "'";
         if (Baglanti.Komut.IntegerVeriOku(sayim, 0) > 0)
             return Baglanti.Komut.DecimalVeriOku(sorgu, 0);
         else
             return 0;

     }

     public List<List_Data> List_Kiraci_Bilgi()
     {
         string daire_kiraci = "SELECT DRNO,CRKOD,OT_ID FROM DAIRE_KIRACI";
         ArrayList ls_drno = Baglanti.Komut.IntegerKoleksiyonEkle(daire_kiraci, 0);
         ArrayList ls_crkod = Baglanti.Komut.MetinKoleksiyonEkle(daire_kiraci, 1);
         ArrayList ls_otid = Baglanti.Komut.IntegerKoleksiyonEkle(daire_kiraci, 2);
         List<List_Data> liste = new List<List_Data>();
         string[] dz_ay = Enum.GetNames(typeof(Aylar));
         for (int i = 0; i < ls_drno.Count; )
         {
             int krdurum = Convert.ToInt32(ls_otid[i]);
             int drno = Convert.ToInt32(ls_drno[i]);
             string crkod = ls_crkod[i].ToString();
             string drad = Baglanti.Komut.MetinselVeriOku("SELECT DRAD FROM DAIRELER WHERE DRNO='" + drno + "'", 0);
             string adi = Baglanti.Komut.MetinselVeriOku("SELECT ADI FROM CARILER WHERE CRKOD='" + crkod + "'", 0);
             string soyadi = Baglanti.Komut.MetinselVeriOku("SELECT SOYADI FROM CARILER WHERE CRKOD='" + crkod + "'", 0);
             decimal kira = Deger_Dondur(crkod, 1);
             decimal deposit = Deger_Dondur(crkod, 2);
             decimal elektrik = Deger_Dondur(crkod, 3);
             decimal su = Deger_Dondur(crkod, 4);
             string blok = Baglanti.Komut.MetinselVeriOku("SELECT BLADI FROM BLOKLAR B, DAIRELER D WHERE B.BLNO=D.BLNO AND D.DRNO='" + drno + "'", 0);


             liste.Add(new List_Data(drno, drad, crkod, adi, soyadi, kira, deposit, elektrik, su, blok, krdurum));

             i++;
         }

         return liste;
     }

 }

 public enum Months
 {
     OCAK = 1,
     ŞUBAT = 2,
     MART = 3,
     NİSAN = 4,
     MAYIS = 5,
     HAZİRAN = 6,
     TEMMUZ = 7,
     AĞUSTOS = 8,
     EYLÜL = 9,
     EKİM = 10,
     KASIM = 11,
     ARALIK = 12
 }
Posted
Comments
[no name] 7-Sep-12 9:06am    
I suppose that List.Sort() does not work for you?
haluk_78 7-Sep-12 9:13am    
No, it didn't work. I use pivotGridControl. I changed it to GridControl so it worked. Now, I am trying to figure it out the reasons...

1 solution

When you call List<t>.Sort(), it uses the "default comparer", but that isn't the one you want. You can specify the comparison though:

C#
private void Sortit()
{
    this.List_Kiraci_Bilgi_Ay.Sort(
        (item1,item2) => {
            return item1.Month.CompareTo(item2.Month);
        }
    );
}
 
Share this answer
 
Comments
haluk_78 8-Sep-12 4:12am     CRLF
Dear dbseman, I am already using " public class List_Data:IComparable<List_Data> " So, I did it already. I solved the problem but I cannot sort the months by alphabetic in pivotgridcontrol. However, I do it in gridcontrol. I couldn't find the solution yet... :(
dbaseman 8-Sep-12 16:15pm     CRLF
You want to sort alphabetically? But is List_Data.Month an Int or a String? If it's an Int, then you probably need to use... return Enum.GetName(typeof(Months), this.Month) .CompareTo(Enum.GetName(typeof(Months), data.Month));
haluk_78 9-Sep-12 2:56am     CRLF
Dear dbaseman, I solved the problem...Yes, the month column is string but I created another column _month as integer and I set the month "sortbysummryinfo" according to _month column...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900