2013-04-03 3 views
0

Je veux fondamentalement lire un fichier texte de mon disque dur, puis utiliser le tri à bulles pour trier les éléments de chaîne dans le fichier texte (en utilisant l'interface IComparable). Je sais que cela pourrait être une question stupide mais je suis coincé ici: Code ci-dessoustrier un fichier texte en utilisant le tri à bulles et IComparable Interface

using System.IO; 

namespace Pratictice 
{ 
    public partial class CfrmPractice : Form 
    { 
     public CfrmPractice() 
     { 
      InitializeComponent(); 
     } 

     private static T[] BubbleSort<T>(T[] list) where T : IComparable 
     { 
      T temp; 
      bool isSorted = false; 

       while (!isSorted) 
       { 
        isSorted = true; 
        for (int i = 0; i < list.Length - 1; i++) 
        { 
         if (list[i].CompareTo(list[i + 1]) > 0) 
         { 
          temp = list[i]; 
          list[i] = list[i + 1]; 
          list[i + 1] = temp; 
          isSorted = false; 

          return (T) Convert.ChangeType(isSorted[bool],typeof(T)); 
         } 
        } 
       } 
     } 


     private void btnSort_Click(object sender, EventArgs e) 
     { 

     } 

     public void LoadDataSource() 
     { 
      ofdLoadData.InitialDirectory = Application.StartupPath; 
      //ofdLoadData.FileName = "Text FIles (.txt)|*.txt|All Files (*.*)|*.*|"; 

      if (ofdLoadData.ShowDialog() == DialogResult.OK) 
      { 
       try 
       { 
        using (StreamReader f = new StreamReader(ofdLoadData.FileName)) 
        { 
         string sLine; 
         string[] Fields; 

         while (!f.EndOfStream) 
         { 
          sLine = f.ReadLine(); 
          Fields = sLine.Split(' '); 
          lstLoadData.Items.Add(Fields[0]); 
         } 
        } 
       } 
       catch (Exception error) 
       { 
        MessageBox.Show(error.Message, "Practice"); 
       } 

      } 
     } 

     private void LoadData_Click(object sender, EventArgs e) 
     { 

      LoadDataSource(); 
     } 

     private void lstLoadData_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     class Compare : IComparable 
     { 
      public string FileName; 

      public int CompareTo(object obj) 
      { 
       if (this.FileName == ((Compare)obj).FileName) 
        return 0; 
       else if (this.FileName != ((Compare)obj).FileName) 
        return 1; 
       else 
        return -1; 
      } 
     } 

    } 
} 
+0

Pourquoi générique? C'est un fichier texte non? Un fichier texte n'est-il pas constitué de 'string's? –

Répondre

1

Eh bien, je vois un peu funkiness passe. Je ne vois pas pourquoi vous avez besoin d'un type de retour lorsque vous modifiez la liste que vous passez dans la méthode. Je ne comprends pas non plus ce que vous essayez d'obtenir avec Convert.ChangeType. Est-ce que ça marche pour toi?

private static void BubbleSort<T>(T[] list) where T : IComparable 
{ 
    T temp; 
    bool isSorted = false; 

    while (!isSorted) 
    { 
     isSorted = true; 
     for (int i = 0; i < list.Length - 1; i++) 
     { 
      if (list[i].CompareTo(list[i + 1]) > 0) 
      { 
       temp = list[i]; 
       list[i] = list[i + 1]; 
       list[i + 1] = temp; 
       isSorted = false; 
      } 
     } 
    } 
} 
Questions connexes