-2

I have a class box with 3 double values. The task is to sort one of the values and print the results. Below there is a code

 public class Box implements Comparable<Box> {

        private double x;
        private double y;
        private double z;
        private double volume;

@Override
    public int compareTo(Box o) {
        return this.x > o.x ? 1 : this.x < o.x ? -1 : 0;
    }

    public static void  sorting (Box[] o)
    {
        System.out.println();
        for (int i = 0; i<o.length; i++)
        {
            System.out.println(o[i].x);
        }
    }

There is no errors in this code, but it is not sorting x parameter too.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
barkman345
  • 1,021
  • 1
  • 7
  • 8
  • You should post your [mcve] so we can see where you actually do the sort, where you call `Arrays.sort(boxes);`, etc. – Hovercraft Full Of Eels Mar 06 '19 at 11:37
  • 5
    well ... you never actually call or implement a sort. what exactly do you expect? your sorting method will just print out what's there, that's all – Stultuske Mar 06 '19 at 11:41
  • oh yeas, thanks, added Arrays.sort(o); in sorting function, ok now – barkman345 Mar 06 '19 at 11:48
  • 2
    You might want to delete this question – Hovercraft Full Of Eels Mar 06 '19 at 12:03
  • If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. [See here](http://meta.stackexchange.com/a/5235) for a full explanation. – dpr Mar 07 '19 at 10:19

3 Answers3

1

Arrays in Java are not automatically sorted (nor are collections in general). For arrays you need to call

Arrays.sort(boxes);

If working with collections you can either sort an existing List with

Collections.sort(boxList);

Or use a SortedSet/Map instead (e.g. TreeSet). These types of collections will automatically order their elements on insertion and maintain the natural order.

As pointed out by @AndyTurner Sets and Maps will of course discard duplicates (depending on your implementation of hashCode and equals) which might be something you don't want.

This question and it's very good answer is also related and provides a good overview of sorting collections in Java in general.

dpr
  • 10,591
  • 3
  • 41
  • 71
1

Implementing Comparable only means that class provides some default order for its instances. It doesn't mean that every array or collection must automatically apply that order.

For instance String class also implements Comparable which provides lexicographical order, but you are still able to decide where your string should be placed in array. It is possible because when you do

array[index] = element;

decision of where element will be placed depends only on index, not on any characteristic of element.

To sort array using default order of its elements you can use Arrays.sort(o);, otherwise your array o will hold elements in positions specified by indexes.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
-1

You actually have few different options. Some of them are creating you own sorting function as it was mentioned, or use a Collection sorting with a proper Comparator. You may want to check this SO topic to get some more details of the topic. Also, remember to use search before you post, it was the first thing to pop out after typing "java sort by object field" or something like that.

Mike Sar
  • 107
  • 9