Advertisement

03.05.2004 at 07:22AM PST, ID: 20908354
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

How to sort a vector of objects.

Tags: vector, sort
Hello experts,

 I am not sure how best to sort a vector of objects I made. The objects in the vector is something I made called a LogError with these properties:

String date
String time
String exception
String className
String methodName
String errorMessage
int lineNumber
int numberOfTimes

Say the vector contains 150 to 1000 LogErrors. The value I want to sort by is the numberOfTimes value from high number to low.

I was messing around with an insertion sort which i think might be best for this situation but I dont really know how to apply it to my vector.

I thought about using an int array to hold an index of the items in my Vector then call my sort algorithm (sort it) and then come back to my vector and try to sort it using the sorted int array holding the new positions.

Here is what I have so far. Is this a good idea or is there something better. Code examples would be great. Thanks!!!!!

int[] sortItem = new int[totalsVector.size()]; //Index array to used to sort the vector.
HashMap totals  = new HashMap();  // thought about using this to hold the index as well.
            
for(int i=0; i<totalsVector.size(); i++)
{
      LogError log = (LogError)totalsVector.get(i);  // Main vector of items to sort //
      int num = log.getNumberOfTimes();
      totals.put(new Integer(i), new Integer(num));
      sortItem[i] = num; // load this with number of times value?
}
            
try{
      insertionSort(sortItem, totalsVector.size() );
      for(int i= 0; i< sortItem.length; i++){
            System.out.println("item= "+ sortItem[i]);
      }
}catch(Exception e){
      e.printStackTrace();
}

public static void insertionSort(int numbers[], int array_size)
{
      int i, j, index;
      for (i=1; i < array_size; i++)
      {
            index = numbers[i];
            j = i;
            while ((j > 0) && (numbers[j-1] > index))
            {
                  numbers[j] = numbers[j-1];
                  j = j - 1;
            }
            numbers[j] = index;
      }
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: entercite
Solution Provided By: mjzalewski
Participating Experts: 6
Solution Grade: A
Views: 345
Translate:
Loading Advertisement...
03.05.2004 at 07:46AM PST, ID: 10523667

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 07:49AM PST, ID: 10523711

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 07:58AM PST, ID: 10523809

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 07:59AM PST, ID: 10523814

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 08:36AM PST, ID: 10524214

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 09:20AM PST, ID: 10524691

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 09:48AM PST, ID: 10524984

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 11:31AM PST, ID: 10525917

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 11:52AM PST, ID: 10526083

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 12:18PM PST, ID: 10526291

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 12:56PM PST, ID: 10526741

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 01:10PM PST, ID: 10526888

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.05.2004 at 03:50PM PST, ID: 10528226

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.09.2004 at 03:45PM PST, ID: 10556083

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 10:58AM PST, ID: 10563739

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 11:04AM PST, ID: 10563801

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 11:10AM PST, ID: 10563873

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 01:57PM PST, ID: 10565544

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 09:12PM PST, ID: 10567871

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.05.2004 at 07:46AM PST, ID: 10523667

Rank: Guru

It would be much simpler and more reasonable to operate on the Vector itself. The insertionSort method could get the Vector passed in and would then look something like:

public static void insertionSort(Vector tv)
{
     int i, j, index;

     int array_size=tv.size();
     for (i=1; i < array_size; i++)
     {
          index = ((LogError)tv.get(i)).getNumberOfTimes();
           j = i;
          while((j>0) && ((LogError)tv.get(j-1)).getNumberOfTimes()>index)
         {
               tv.set(j,tv.get(j-1));
               j = j - 1;
          }
          tv.set(j,tv.get(i));
     }
}
 
03.05.2004 at 07:49AM PST, ID: 10523711

Rank: Guru

Sorry, looks like you would have to save the original object too:

public static void insertionSort(Vector tv)
{
     int i, j, index;
     LogError tmp;

     int array_size=tv.size();
     for (i=1; i < array_size; i++)
     {
          tmp=(LogError)tv.get(i);
          index=tmp.getNumberOfTimes();
           j = i;
          while((j>0) && ((LogError)tv.get(j-1)).getNumberOfTimes()>index)
         {
               tv.set(j,tv.get(j-1));
               j = j - 1;
          }
          tv.set(j,tmp);
     }
}
 
03.05.2004 at 07:58AM PST, ID: 10523809

Rank: Genius

You just need a Comparator

Collection.sort(yourVector, new LogComparator());


................................



      class LogComparator implements java.util.Comparator {
            public int compare(Object o1, Object o2) {
                  LogError le1 = LogError(o1);
                  LogError le2 = LogError(o2);
                  return le2.getNumberOfTimes() - le1.getNumberOfTimes();
            }
      }
 
03.05.2004 at 07:59AM PST, ID: 10523814

Rank: Genius

Typo

>>Collection.sort(yourVector, new LogComparator());

should be

Collections.sort(yourVector, new LogComparator());
 
03.05.2004 at 08:36AM PST, ID: 10524214
Make your LogError class implement Comparable

public class LogError implements Comparable {
  public int lineNumber;
  public int numberOfTimes;
  ...
  // negative if this < oCompare. positive if this > oCompare
  public int compareTo( Object oCompare) {
      LogError leCompare = (LogError) oCompare;
      if( leCompare.numberOfTimes == this.numberOfTimes) {
        return this.lineNumber - leCompare.lineNumber;
      }
      return leCompare.numberOfTimes - this.numberOfTimes;
  }
}

Now just use the java.util.Collections class to sort your Vector

  Collections.sort( totalsVector);

Even better, put your LogError items into a TreeSet class instead of a Vector, then TreeSet.iterator() returns the LogError elements in sorted order.
 
03.05.2004 at 09:20AM PST, ID: 10524691

Rank: Wizard

Use java.util.TreeSet instead of Vector. Elements will already be sorted.
 
03.05.2004 at 09:48AM PST, ID: 10524984

Rank: Wizard

Sorry, i didn't see the end of your comment, mjzalewski.
 
03.05.2004 at 11:31AM PST, ID: 10525917

Rank: Genius

>>Use java.util.TreeSet instead of Vector. Elements will already be sorted.

Not without implementing Comparable. And if you implement it as mjzalewski has outlined, you've effectively 'hard-coded' its natural ordering, which is probably undesirable.

Using a Comparator does not tie you to one criterion of ordering

 
03.05.2004 at 11:52AM PST, ID: 10526083
  So far I made the idea mjzalewski  suggested, using the Comparable value. I dont like the idea of being locked into one sort type but in the short term it works.

  Tried using the insertionSort imladris fixed but that did not sort the list at all?

  CEHJ-
    I am a bit confused about your idea. Is that all the code I need to test it or do I have to rewrite my insert sort using that compare method?
 
  If I use the java.util.TreeSet how does it know to sort my LogErrors based on the numberOfTimes value on the LogError?

 
03.05.2004 at 12:18PM PST, ID: 10526291

Rank: Genius

> I dont like the idea of being locked into one sort type but in the short term it works.

That's fine, you have just defined the natural ordering for your LogError objects.

You can then create seperate Comparator instances to define additional sort orders, and pass that to your sort method.

>  do I have to rewrite my insert sort using that compare method?

You could change you sort method to also accept a Comparator instance, and use it to do the comparison (if providied). This would enable doing different sort orders.

> If I use the java.util.TreeSet how does it know to sort my LogErrors based on the numberOfTimes value on the LogError?

If you don't provide it with a Comparator on construction it uses the natual sort order defined by Comparable.
 
03.05.2004 at 12:56PM PST, ID: 10526741

Rank: Genius

>>Is that all the code I need to test it

Yes. Java will do the sorting for you
 
03.05.2004 at 01:10PM PST, ID: 10526888

Rank: Genius

Just noticed a couple of typos in my example. This is better:

      class LogComparator implements java.util.Comparator {
            public int compare(Object o1, Object o2) {
                  LogError errorOne = (LogError)o1;
                  LogError errorTwo = (LogError)o2;
                  return errorTwo.getNumberOfTimes() - errorOne.getNumberOfTimes();
            }
      }
 
03.05.2004 at 03:50PM PST, ID: 10528226

Rank: Master

I had a similar problem a while ago and give you a short extract of the guidelines i got from about the same people quoting in this thread
, and it works perfectly

You just have to replace the file object with your logrecord and voila...

class FileDateComparator implements Comparator {

     public int compare(Object o1, Object o2) {
          if (!(o1 instanceof File) || !(o2 instanceof File)) {
               throw new IllegalArgumentException();
          }
          File f1 = (File)o1;          
          File f2 = (File)o2;
          if (f1.lastModified()<f2.lastModified()) {
               return 1;
          }
          else if (f1.lastModified()>f2.lastModified()) {
               return -1;
          }
          return 0;
     }
}

Use the implementation like so:

TreeSet fileSet = new TreeSet(new FileDateComparator());
// Add files to fileSet

Iterator fileIt = fileSet.iterator();
while (fileIt.hasNext()) {
// do stuff to the files or something!
}
 
03.09.2004 at 03:45PM PST, ID: 10556083
To use a TreeSet, you must have things in the set that implement Comparable. Or, you can supply a Comparator class in the constructor.

Make a LogComparator object like CEHJ suggested

  LogComparator lcSorter = new LogComparator();

Then pass the Comparator to the TreeSet, and send your Vector at the TreeSet you create (or get rid of the Vector entirely, and just add LogError items one at a time to the TreeSet.

  TreeSet tsSorted = new TreeSet( lcSorter);
  tsSorted.addAll( totalVectors);

If you want to make the class implement Comparable, but would like to be able to set different sort orders, you could do that in various ways.

Perhaps you could use both ideas. Add a static field which contains a Comparator to LogError. Then use it in the compareTo() method. Even simpler (though less object oriented) would be to use a static int field. Here is an example:

public class LogError implements Comparable {
  public int lineNumber;
  public int numberOfTimes;
  public String date;

  public static int sortBy = LogError.SORT_BY_LINE_NUMBER;
  public static final int SORT_BY_DATE = 1;
  public static final int SORT_BY_LINE_NUMBER = 2;
  // ... add more sort types

  // negative if this < oCompare. positive if this > oCompare
  public int compareTo( Object oCompare) {
      LogError leCompare = (LogError) oCompare;
    public int compareTo( Object oCompare) {
      LogError leCompare = (LogError) oCompare;
      switch( LogError.sortBy) {
         case SORT_BY_LINE_NUMBER:

          if( leCompare.numberOfTimes == this.numberOfTimes) {
            return this.lineNumber - leCompare.lineNumber;
          }
          return leCompare.numberOfTimes - this.numberOfTimes;

         case SORT_BY_DATE:
          if( leCompare.date == this.date) {
            return this.lineNumber - leCompare.lineNumber;
          }
          return this.date.compareTo( leCompare.date);
      }
        ...
      return 0;
    }
}

  Now just do LogError.sortBy = LogError.SORT_BY_DATE;

> I dont like the idea of being locked into one sort type but in the short term it works.

Of course it works. I actually tried it on my own machine before I submitted it.
 
03.10.2004 at 10:58AM PST, ID: 10563739
mjzalewski,

I got an error with this code:

: illegal start of expression
public int compareTo( Object oCompare)


      public int compareTo( Object oCompare)
      {
            LogError leCompare = (LogError) oCompare;
            public int compareTo( Object oCompare)
            {
                  LogError leCompare = (LogError) oCompare;
                  switch( LogError.sortBy )
                  {
                        case SORT_BY_LINE_NUMBER:
                        if( leCompare.numberOfTimes == this.numberOfTimes) {
                              return this.numberOfTimes - leCompare.numberOfTimes;
                        }
                        return leCompare.numberOfTimes - this.numberOfTimes;

                        case SORT_BY_DATE:
                        if( leCompare.date == this.date) {
                              return this.numberOfTimes - leCompare.numberOfTimes;
                        }
                        return this.date.compareTo( leCompare.date);
                  }
                  
                  return 0;
            }
      }
 
03.10.2004 at 11:04AM PST, ID: 10563801
I think you meant:

      public int compareTo( Object oCompare)
      {
            LogError leCompare = (LogError) oCompare;
            switch( LogError.sortBy )
            {
                  case SORT_BY_LINE_NUMBER:
                  if( leCompare.numberOfTimes == this.numberOfTimes) {
                        return this.numberOfTimes - leCompare.numberOfTimes;
                  }
                  return leCompare.numberOfTimes - this.numberOfTimes;

                  case SORT_BY_DATE:
                  if( leCompare.date == this.date) {
                        return this.numberOfTimes - leCompare.numberOfTimes;
                  }
                  return this.date.compareTo( leCompare.date);
            }
            
            return 0;
      }
 
03.10.2004 at 11:10AM PST, ID: 10563873

Rank: Genius

The if statements are redundant. This will suffice for the first type of sort

>>return leCompare.numberOfTimes - this.numberOfTimes;

You should use private scoped fields though, giving you

return leCompare.getNumberOfTimes() - this.numberOfTimes;

You can also parameterize on ascending/descending order of course

 
03.10.2004 at 01:57PM PST, ID: 10565544

Rank: Genius

Your code will be a lot cleaner using seperate Comparator implementations for seperate sort orders.

public class SortByLineNumber implements Comparator
{
    public int compareTo( Object oCompare)
     {
          LogError leCompare = (LogError) oCompare;
          return this.numberOfTimes - leCompare.numberOfTimes;
     }
}

public class SortByDate implements Comparator
{
    public int compareTo( Object oCompare)
     {
          LogError leCompare = (LogError) oCompare;
               if( leCompare.date == this.date) {
                    return this.numberOfTimes - leCompare.numberOfTimes;
               }
               return this.date.compareTo( leCompare.date);
     }
}
 
03.10.2004 at 09:12PM PST, ID: 10567871
Actually, I meant

// negative if this < oCompare. positive if this > oCompare
  public int compareTo( Object oCompare) {
      LogError leCompare = (LogError) oCompare;
      switch( LogError.sortBy) {
         case SORT_BY_LINE_NUMBER:

          if( leCompare.numberOfTimes == this.numberOfTimes) {
            return this.lineNumber - leCompare.lineNumber;
          }
         return leCompare.numberOfTimes - this.numberOfTimes;

         case SORT_BY_DATE:
          if( leCompare.date == this.date) {
            return this.lineNumber - leCompare.lineNumber;
          }
          return this.date.compareTo( leCompare.date);
      }
        ...
      return 0;
    }

like I wrote in the beginning. The 'if' statements as I wrote are not redundant. But it looks like the first two lines of the compareTo method got doubled.

The way I have written it, it sorts descending by numberOfTimes, then ascending by lineNumber if numberOfTimes is a match. But there are many other possibilities, each of which you could implement by adding a new segment to thw switch statement.

You could also do like objects suggests, and implement one new class for each way that you want to sort. Is it cleaner? Well I guess it's a matter of opinion. It's certainly more class files, or at least more classes. But on the other hand, you can implement new sort criteria simply by creating a new class, and you wouldn't have to break the code in the nasty switch statement -- well, it could get nasty if you have lots of different ways to sort.

If you still have trouble with the code I posted, I could send you the file I used to test. Just a single .java file with a public static void main() that makes three LogError objects and sorts em. Unfortunately, its on my other computer as I write this.
Accepted Solution
 
 
20080236-EE-VQP-29