0

I got a question about sorting an array.

i got an array like this:

long[][] allDate = new long[lenght][2];

and the valus are: allDate[][0] represend date in miliseconds and allDate[][1] represend value in this case its price of date from before for example

allDate[0][0] = 142142141
allDate[0][1] = 90

allDate[][0] value of date is always in future from now and what i want to do is to sort this dates in ascending chronological logic. but the problem is i want to keep their price so i could do add it to my Jfreechart line chart.

in this loop

    for (int i = 0; i < a7; i++) {
        int day = (int) (new Date().getTime() + allDate[i][0] / (1000 * 60 * 60 * 24));
        int month = (int) (new Date().getTime() + allDate[i][0] / (1000 * 60 * 60));
        int year = (int) (new Date().getTime() + allDate[i][0] / (1000 * 60));
        s1.add(new Day(day, month, year), (eWallet + allDate[i][1]));
    }

any ideas how to sort it ?

1 Answer 1

4

and the valus are: allDate[][0] represend date in miliseconds and allDate[][1] represend value in this case its price of date from before

So the first thing I'd do is avoid storing those as a 2D array. Instead, store a 1D collection (whether that's an array or a list) of the pair of values:

PriceSnapshot[] prices = ...;

Where PriceSnapshot would consist of a timestamp (whether that's as a long, a Date, a Joda Time Instant etc or whatever) and a price.

At that point, you can easily write a Comparer<PriceSnapshot> to compare by time, and sort using that. Of course you could write a Comparer<long[]> instead, but that wouldn't be nearly as nice.

(Also note that with your current loop, you're taking "the current time" 3 times, which means you could end up with different dates. Bad idea. Take a single snapshot of "now" and then use that repeatedly.)

Sign up to request clarification or add additional context in comments.

13 Comments

@user2160958: Which part? I've suggested multiple steps here. The very first thing to do is create a new class to represent the two values you're talking about. Do you understand that part?
so the class contain to fields (array or list) ? and i store data for one object ?
@user2160958: The class would just have two fields - the timestamp and the price for one entry. That way you can avoid creating a 2D array.
and something like this would work ? <pre>Collections.sort(allDate, new Comparator<Long[]>() { public int compare(Long[] o1, Long[] o2) { return o2[0].compareTo(o1[0]); } });</code>
@user2160958: No, because you've got a long[] rather than a Long[]. They're different. After that change, it would work - but it would be nasty code. You should really, really get away from storing these in a 2D array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.