Results 1 to 5 of 5
  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Put and order values in a List

    I have a class which compares a set of times (using JodaTime Lib) to find the closest one to now:
    Java Code:
    public class TimeComparitor {
    
    private List<LocalTime> targetTimes;
    
    public TimeComparitor() {
        targetTimes = new ArrayList<LocalTime>();
    }
    
    public void addTargetTime(LocalTime target) {
        targetTimes.add(target);
    }
    
    public LocalTime getClosestFutureTarget(DateTime now) {
        LocalTime result = null;
        long resultOffset = Long.MAX_VALUE;
        ArrayList<String> bigger = new ArrayList<String>();
    
        for (LocalTime target : targetTimes) {
            DateTime todayTarget = target.toDateTimeToday();
            long differenceMillis = todayTarget.getMillis() - now.getMillis();
            if (differenceMillis > 0 && differenceMillis < resultOffset) {
                resultOffset = differenceMillis;
                result = target;
            } 
        }
        return result;
    }
    As you see I could catch the closest time by getClosestFutureTarget method, *But I not only want the closest one, but also an ordered list of all Times greater than now.
    The question is: First, how to save them in a list? Should it be through getClosestFutureTarget?

    Second, to sort the list from closest to farthest I'm told to use something like this:
    Java Code:
    public class LocalTime implements Comparable<LocalTime> {
        ...
        public int compareTo(LocalTime o){
             // compare "this" to o
        }
    }
    and then using Collections.sort. But not sure how to adapt it to my code.Just define this class in the same file for Previous class? Or creat a new class file? What should be inside class and where to call Collections.sort.

    Thanks in advance
    Last edited by ghandil; 06-01-2013 at 08:30 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Put and order values in a List

    Cross posted and didn't return to the discussion once pointed to the tutorials:
    java - Get two different types of both data and structure from a method - Stack Overflow

    The code apparently came from an earlier discussion:
    Discussion between GreyBeardedGeek and DummyBeginner - 2013-05-21

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Put and order values in a List

    Quote Originally Posted by DarrylBurke View Post
    Cross posted and didn't return to the discussion once pointed to the tutorials:
    java - Get two different types of both data and structure from a method - Stack Overflow

    The code apparently came from an earlier discussion:
    Discussion between GreyBeardedGeek and DummyBeginner - 2013-05-21

    db
    What should I do now? I've read Comparable (Java Platform SE 6) mentioned in Stackoverlow's post, But Couldn't adapt to my code, So I came here to New Java forum.
    Thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Put and order values in a List

    Quote Originally Posted by ghandil View Post
    What should I do now? I've read Comparable (Java Platform SE 6) mentioned in Stackoverlow's post, But Couldn't adapt to my code, So I came here to New Java forum.
    Thanks
    Ask your question in the stack overflow forum? It is of no use to switch forums all the time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Put and order values in a List

    Ok, Would you please Delete this thread, I don't want to start my activity on the Forum with a wrong topic

Similar Threads

  1. Replies: 9
    Last Post: 05-12-2011, 10:51 PM
  2. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  3. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  4. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 07:59 AM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •