[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

Sorting an array by date/time

I'm fairly new to Java, so I'll try and explain what I need properly.

I've got a frame, which reads a text containing log entries.  However, the log entries will not be in any kind of order, but when I display them, I'd like them to be in date/time order.

An example entry would be:-

31/07/2004-10:00-Log entry.
15/07/2004-09:00-Another log entry.

So, how can I display those entries (note the log file could contain an infinate number of entries) in a list box in my frame? You may need to supply all of the code for the frame/file reading bit - although I can do it myself, it could cause problems inserting code into the mess I've already created!

Finally, this will be inside another program, so I already have a main function. The screen will be called from other methods in other java files.

Hope I've explained it properly. If not, just let me know.

Thanks,

Adrian Johnson
0
adrianjohnson
Asked:
adrianjohnson
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
objectsCommented:
Use Arrays.sort() method to sort your array, using a custom Comparator that does the date/time comparison (or Collections.sort() if in a collection)
0
 
CEHJCommented:
You need to parse your file into classes that can be made into entries in a List model. Run this - the entries have been hard-coded. You will supply yours from file:

import java.text.SimpleDateFormat;
import java.util.Date;

public class LogEntry implements Comparable {
      private final static String DATE_FORMAT_STRING = "dd/MM/yyyy-HH:mm";
      private final static int DATE_PARSE_OFFSET = 16;
      private String datePart;
      private String entry;
      private Date date;
      private SimpleDateFormat format;

      public LogEntry(String entry) {
            format = new SimpleDateFormat(DATE_FORMAT_STRING);
            parse(entry);
      }

      public void setDatePart(String datePart) {
            this.datePart = datePart;
      }

      public String getDatePart() {
            return datePart;
      }

      public void setEntry(String entry) {
            this.entry = entry;
      }

      public String getEntry() {
            return entry;
      }

      public Date getDate() {
            return date;
      }

      public String toString() {
            return entry;
            // or the following for just the date
            //return datePart;
      }

      private void parse(String entry) {
            this.entry = entry;
            datePart = entry.substring(0, DATE_PARSE_OFFSET);
            try {
                  date      = format.parse(datePart.trim());
            }
            catch(Exception e) {
                  e.printStackTrace();
                  System.out.println("Date part is: " + datePart);
            }
      }

      public int compareTo(Object other) {
            LogEntry otherEntry = (LogEntry)other;
            return date.compareTo(otherEntry.getDate());
      }

      // TESTING PURPOSES ONLY!
      public static void main(String[] args) {

            String entry1 = "31/07/2004-10:00-Log entry";
            String entry2 = "15/07/2004-09:00-Another log entry";
            String entry3 = "15/07/2004-08:15-Another log entry from earlier the same day";

            java.util.Vector v = new java.util.Vector();
            v.add(new LogEntry(entry1));
            v.add(new LogEntry(entry2));
            v.add(new LogEntry(entry3));

            java.util.Collections.sort(v);
            System.out.println(v);

            javax.swing.JFrame frame = new javax.swing.JFrame();
            frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            javax.swing.DefaultComboBoxModel model = new javax.swing.DefaultComboBoxModel(v);
            javax.swing.JList list = new javax.swing.JList(model);
            frame.getContentPane().add(list, java.awt.BorderLayout.CENTER);
            frame.setSize(100, 100);
            frame.setVisible(true);

      }

}
0
 
adrianjohnsonAuthor Commented:
Thanks for the entries so far. I'm a little busy at the moment, but will try them and see what happens.

Adrian Johnson
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
adrianjohnsonAuthor Commented:
I'm working through your code, CEHJ, and changed the name of the main function to DisplayLog.  Having problems calling it, though, from another class:

LogEntry logscreen = new LogEntry();
logscreen.DisplayLog();

The error I get is "The contructor LogEntry is undefined". Is it something I'm missing/doing wrong? It's probably my understanding (it's very hot here in the UK, and I can't concentrate!).

Thanks,

Adrian Johnson
0
 
objectsCommented:
try:

LogEntry.DisplayLog();

You can sort your entries with or without using a seperate class to represent an entry.
The key is using the sort() method and providing a method to handle xomparing two entries.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you have provided a parameterized constructor in the LogEntry class, then you will need to give a default constructor (with no parameters) too, otherwise you cannot instantiate it without arguments, like:

LogEntry logscreen = new LogEntry () ;

If the DisplayLog () method is still static, you don't need an object for calling it and you can do it directly using the class-name, like objects has shown.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
objects, are you still listening to: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21080150.html ?

CEHJ, can I have some inputs from you on that one please, if you want to say something?
0
 
adrianjohnsonAuthor Commented:
Now I'm getting confused, as it appears I'm getting two different methods of calling the class (and neither of them work).  I renamed the main function DisplayLog(), so it looks like:

public void DisplayLog() {

          String entry1 = "31/07/2004-10:00-Log entry";
          String entry2 = "15/07/2004-09:00-Another log entry";
          String entry3 = "15/07/2004-08:15-Another log entry from earlier the same day";

          java.util.Vector v = new java.util.Vector();
          v.add(new ScreenDisplay(entry1));
          v.add(new ScreenDisplay(entry2));
          v.add(new ScreenDisplay(entry3));

          java.util.Collections.sort(v);
          System.out.println(v);

          javax.swing.JFrame frame = new javax.swing.JFrame();
          frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
          javax.swing.DefaultComboBoxModel model = new javax.swing.DefaultComboBoxModel(v);
          javax.swing.JList list = new javax.swing.JList(model);
          frame.getContentPane().add(list, java.awt.BorderLayout.CENTER);
          frame.setSize(100, 100);
          frame.setVisible(true);

     }

How Do I call it from another function in another class?  If I use mayankealges code, I get "The Constructor LogEntry is undefined".  If I use objects code, I get "The method DisplayLog is not static".

Remember, I'm fairly new to Java (and baking hot here in the UK), so you need to be really specific.

Thanks,

Adrian Johnson
0
 
objectsCommented:
> public void DisplayLog() {

make it static:

public static void DisplayLog() {
0
 
adrianjohnsonAuthor Commented:
D'oh!

Thanks Objects.  Sorry I'm so dim, but that worked a treat!


Adrian Johnson
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now