ArrayList Sorting Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freddieMaize
    New Member
    • Aug 2008
    • 85

    ArrayList Sorting Problem

    Hi Guys,

    I have an arraylist named c like the below,
    Code:
    [654,6465,98798,321123,874,654645];
    I sort it by converting it into a int array
    Code:
    Arrays.sort(c);
    But then, i have a arraylist like below,
    Code:
    [google,5462,yahoo.2245,aol,8896]
    The above array is something like, [name of the site,response time,name of the site,response time,...]

    All i want to do is to sort the arrylist with respect to the response time and arrange the List accordingly.

    A sample output am looking for is like below,
    Code:
    [yahoo,2245,google,5462,aol,8896]
    Here yahoo is in the first element becasue its response time is the least

    All i'm trying to do is to access different sites and find out its response time and finally display the sites with the least responding time as the first and then the next least and so on...

    Kindly suggest me a way to sort this.. Thank you...

    Regards,
    Freddie
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    So you basically rip an entity apart (a search engine and its response time) and
    stick the remnants in an ArrayList and assume that list to be clever enough to
    know what you mean by those names and numbers? Computers are way too
    stupid for that; design a proper little class for that entity instead and make it
    Comparable so that it can be sorted (and you don't need arrays for it).

    kind regards,

    Jos

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by JosAH
      ...design a proper little class for that entity instead and make it
      Comparable so that it can be sorted (and you don't need arrays for it).
      You can then make an ArrayList of the comparable Objects if you wish and sort them with Collections.sor t(). Of course, you're free to choose any other way of collecting and sorting the Objects too apart from an ArrayList.

      Greetings,
      Nepomuk

      Comment

      • freddieMaize
        New Member
        • Aug 2008
        • 85

        #4
        Thanks guys... i solved my problem...

        Below is the code snippet that i used...

        Code:
        abean.setName(urlArray[i]);  //setting the name of the site
        abean.setValue(Long.valueOf(responseTime));  //setting its response time
        responseTimeList.add(abean);
        Code:
        Collections.sort(responseTimeList, new Comparator() {
        public int compare(Object arg0, Object arg1) {
                        ABean obj1 = (ABean) arg0;
                        ABean obj2 = (ABean) arg1;
                  return obj1.getValue().compareTo(obj2.getValue());
                    }
                    });
        Thank you,
        Freddie

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Yup, that's the way to do it or make your bean class implement the Comparable
          interface so that you don't need that Comparator for the Collections.sor t() method.
          But that doesn't matter much; well done.

          kind regards,

          Jos

          Comment

          • freddieMaize
            New Member
            • Aug 2008
            • 85

            #6
            Originally posted by JosAH
            Yup, that's the way to do it or make your bean class implement the Comparable
            interface so that you don't need that Comparator for the Collections.sor t() method.
            But that doesn't matter much; well done.

            kind regards,

            Jos
            Oh yeah... Thank you JosAh...

            I was also wondering how to do this without all these Comparable interface or the sort()... I thought of below,

            Because I know that you have to sort based on values at "even" positions of array, and I can directly access values of an array based on index,

            Implement any sorting algorithm (eg: bubble sort) comparing values at even positions and when I swap the values, I swap not only values at x,y positions, but also swap x-1,y-1 positions at the same time.


            However this wont be good for any application oriented work thought... This may be useful for some Assigment works :D

            Cheers,
            Freddie

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by freddieMaize
              I was also wondering how to do this without all these Comparable interface or the sort()... I thought of below,

              Because I know that you have to sort based on values at "even" positions of array, and I can directly access values of an array based on index,

              Implement any sorting algorithm (eg: bubble sort) comparing values at even positions and when I swap the values, I swap not only values at x,y positions, but also swap x-1,y-1 positions at the same time.
              That should work, although it's not at all a Object orientated solution (while the one you're using now is). If you want to do it without Objects, you could of course also have a 2D array like this:[code=java]private String[][] myArray = new String[2][4]; // 4 Websites with both names and response times as Strings
              ...
              private void swap(int a, int b)
              {
              String tmp = myArray[0][a];
              myArray[0][a] = myArray[0][b];
              myArray[0][b] = tmp;

              tmp = myArray[1][a];
              myArray[1][a] = myArray[1][b];
              myArray[1][b] = tmp;
              }[/code]But if possible, you should always prefer using Objects, like you do now.

              Greetings,
              Nepomuk

              Comment

              Working...