Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Υλοποίηση Interface και ταξινόμηση σε ArrayList

Îåêßíçóå áðü ôï ìÝëïò xabikos. Τελευταία δημοσίευση από το μέλος γιωργος μπακογιαννης στις 15-06-2007, 06:01. Υπάρχουν 4 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  13-06-2007, 23:25 32906

    Υλοποίηση Interface και ταξινόμηση σε ArrayList

    Λοιπόν το πρόβλημα μου είναι ότι δεν έχω καταλάβει πως ακριβώς πρέπει να υλοποιήσω το Interface IComparer για να επιτύχω σωστή ταξινόμηση μέσα σε ένα ArrayList. Έχω δει το άρθρο στο blog του Μάνου αλλά δεν έβγαλα άκρη μιας και δεν ξέρω καθόλου Basic. Αναλυτικότερα έχω την εξής κλάση

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    __gc class Document {
    private:
      String* name;
      String* docID;
      int numberWords;
      double score;

    public:
      Document(){}
      Document(int numWords, String* nm, String* id)
      {numberWords = numWords;
       name = nm;
       docID = id;
       score =0;}

      void setScore(double scr)
      {score = scr;}

      void setScoreZero()
      {score=0;}

      double getScore()
      {return score;}

      __gc class DocumentScoreCompare : public IComparer
      {
      public: int Compare(Object* doc1,Object* doc2)
        {
          Document* document1 = dynamic_cast<Document*>(doc1);
          Document* document2 = dynamic_cast<Document*>(doc2);
          //return document1->score.ToString()->CompareTo(document2->score.ToString());
          if(document1->score >= document2->score)
            return -1;
          else
            return 1;
        }
      };
    };
    όπου μέσα υλοποιώ το interface IComparer σύμφωνα με αυτά που βρήκα στο net και στο documantation.
    Τα αντικείμενα τις παραπάνω κλάσης τα χρησιμοποιώ σε αυτή.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    __gc class MainProgram {
    private:
      BplusTree* invertedFile;
      String *dirPath, *docPath;
      ArrayList* documents;


    public:
      MainProgram()
      {
        invertedFile = new BplusTree(64);
        dirPath = S"c:\\irSystem";
        docPath = S"c:\\irSystem\\documents";
        documents = new ArrayList();

        if(!Directory::Exists(dirPath))
        {
          Directory::CreateDirectory(dirPath);
          Directory::CreateDirectory(docPath);
        }
      }

    ArrayList* DocumentRanking(ArrayList* keywords)
      {
        documents->Sort(new Document::DocumentScoreCompare());
      }

    };
    Κατά την εκτέλεση του προγράμματος προσθέτω αρκετά αντικείμενα τύπου Document στο συγκεκριμένο ArrayList.  Αυτό που δεν μπορώ να πετύχω είναι να ταξινομηθούν να αντικείμενα σύμφωνα με το score που αυτά έχουν. Και αυτό που μου έκανε εντύπωση και δεν μπόρεσα να καταλάβω είναι ο τρόπος που καλείτε η compare. Καμιά ιδέα;;

    My dream is to fly over the rainbow so high!!!!
  •  14-06-2007, 07:25 32909 σε απάντηση της 32906

    Απ: Υλοποίηση Interface και ταξινόμηση σε ArrayList

    Θα μπορούσε να χρησιμοποιηθεί αντί «Collections», «Collections.Generic» που strong type το IComparer.

     

    public class Document
        {
            string docId;
            string name;
            int numberWords;
            double score;

            public Document() {
            }
            public Document(string name, string docId, int numberWords) {
                this.name = name;
                this.docId = docId;
                this.numberWords = numberWords;
                score = 0;
            }
            public string DocId {
                get { return docId; }
                set { docId = value; }
            }
            public string Name {
                get { return name; }
                set { name = value; }
            }
            public int NumberWords {
                get { return numberWords; }
                set { numberWords = value; }
            }
            public double Score {
                get { return score; }
                set { score = value; }
            }
            public void setScoreZero() {
                Score = 0;
            }
            public override string ToString() {
                return string.Format(CultureInfo.CurrentCulture, "{0}\t{1:N}", Name, Score);
            }
        }

        public class DocumentComparer : IComparer
        {
            #region IComparer Members
            public int Compare(object x, object y) {
                if (x == null) return y == null ? 0 : -1;
                if (y == null) return 1;
                if (((Document)x).Score > ((Document)y).Score) return 1;
                if (((Document)x).Score < ((Document)y).Score) return -1;
                return 0;
            }
            #endregion
        }

     

        static void Main() {           
                Random numberWordsRandom = new Random();
                Random scoreRandom = new Random();
                ArrayList documents = new ArrayList();
                for (int i = 0; i < 20; i++) {
                    Document document = new Document("Book" + i.ToString(CultureInfo.InvariantCulture), Guid.NewGuid().ToString(), numberWordsRandom.Next(1500));
                    document.Score = scoreRandom.Next(100);
                    document.Score /= scoreRandom.Next(23) + 1;
                    documents.Add(document);
                }
                documents.Sort(new DocumentComparer());
                foreach (Document document in documents)
                    Console.WriteLine(document);
        }


    while (!dead) learn();
  •  14-06-2007, 07:42 32910 σε απάντηση της 32906

    Απ: Υλοποίηση Interface και ταξινόμηση σε ArrayList

    Και ένα παράδειγμα σε LINQ, χωρίς τη IComparer Smile

        public class Document
        {
            string docId;
            string name;
            int numberWords;
            double score;

            public Document() {
            }
            public Document(string name, string docId, int numberWords) {
                this.name = name;
                this.docId = docId;
                this.numberWords = numberWords;
                score = 0;
            }
            public string DocId {
                get { return docId; }
                set { docId = value; }
            }
            public string Name {
                get { return name; }
                set { name = value; }
            }
            public int NumberWords {
                get { return numberWords; }
                set { numberWords = value; }
            }
            public double Score {
                get { return score; }
                set { score = value; }
            }
            public void setScoreZero() {
                Score = 0;
            }
            public override string ToString() {
                return string.Format(CultureInfo.CurrentCulture, "{0}\t{1:N}", Name, Score);
            }
        }

     

            static void Main(string[] args)
            {
                Random numberWordsRandom = new Random();
                Random scoreRandom = new Random();
                List<Document> documents = new List<Document>();
                for (int i = 0; i < 20; i++)
                {
                    Document document = new Document("Book" + i.ToString(CultureInfo.InvariantCulture), Guid.NewGuid().ToString(), numberWordsRandom.Next(1500));
                    document.Score = scoreRandom.Next(100);
                    document.Score /= scoreRandom.Next(23) + 1;
                    documents.Add(document);
                }

                
                foreach (Document document in from doc in documents orderby doc.Score descending select doc)
                    Console.WriteLine(document);


            }


    while (!dead) learn();
  •  15-06-2007, 02:11 32932 σε απάντηση της 32909

    Απ: Υλοποίηση Interface και ταξινόμηση σε ArrayList

    Σ ευχαριστώ πολύ για τις απαντήσεις σου. Την πρώτη την κατάλαβα σχεδόν όλη. Απλά η μόνη αλλαγή που έκανα ήταν στις τιμές που επιστρέφει όταν συγκρίνει δύο document γιατί έτσι έκανε αύξουσα ταξινόμηση ενώ εγώ ήθελα φθίνουσα. Επίσης όταν έβαλα τα #region IComparer Members και #endregion μου έβγαζε λάθος μεταγλώττισης. Επίσης δεν κατάλαβα τι κάνει αυτό το public override string ToString() {return string.Format(CultureInfo.CurrentCulture, "{0}\t{1:N}", Name, Score);} αλλά δεν είναι η πρώτη μου φορά εδώ μέσα. Πολύ συχνά διαβάζω τα posts σας και αναρωτιέμαι αν φτάσω ποτέ σε τέτοιο επίπεδο. Tongue Tied

    My dream is to fly over the rainbow so high!!!!
  •  15-06-2007, 06:01 32935 σε απάντηση της 32932

    Απ: Υλοποίηση Interface και ταξινόμηση σε ArrayList

    Για τη φθίνουσα ταξινόμηση, απλά αλλάζεις τα πρόσημα:

        public class DescendingDocumentComparer : IComparer
        {
            #region IComparer Members
            public int Compare(object x, object y) {
                if (x == null) return y == null ? 0 : 1;
                if (y == null) return -1;
                if (((Document)x).Score > ((Document)y).Score) return -1;
                if (((Document)x).Score < ((Document)y).Score) return 1;
                return 0;
            }
            #endregion
        }

    Για τα  block of codes (#region .... #endregion) πιστεύω ότι είναι προφανές ότι είναι σε C#

    Για τη ToString():

    http://msdn2.microsoft.com/en-us/library/ms173154(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/1ksz8yb7.aspx

    Και για το τελευταίο: χαλαρά


    while (!dead) learn();
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems