0

I am trying to sort my table by the sales figures.
For example:

Names          Figures
A 400
B 200
C 500
will be
Names          Figures
C 500
A 400
B 200
after being sorted. This is my code. I am a beginner pardon my code hehe

Scanner input = new Scanner(System.in);
int numAsso;
System.out.print("Enter the Number of Associates: ");
numAsso = input.nextInt();

String[] names = new String[numAsso];
String line;

for (int i = 0; i < numAsso; i++)
{
System.out.print("Enter the name of the Associate: ");
names[i] = input.next();
}

 final Double[][] sales = new Double [numAsso][2];
double sum= 0;
for(int j = 0; j < numAsso; j++)
{
System.out.print("Enter Total Figures for "+ names[j]+": ");
sales[j][0] = input.nextDouble();
sum+=sales[j][0];
}
double average = sum/numAsso;



System.out.println("Names\t\t"+"Figures\t\t"+"Average");

for(int x = 0; x < numAsso; x++)
{
System.out.println(names[x] + "\t\t" + sales[x][0] + "\t\t" + average);

}
2
  • You could use Map instead of Array something like this Map<String, Double> map = new TreeMap<String, Double>(yourMap); Commented Feb 16, 2017 at 14:43
  • Can you give short code for your array? Commented Feb 16, 2017 at 14:45

2 Answers 2

4

You should store your data in a Map and sort it by values, you can take a look to these links:

Utility Class approach

Instead of a static sorter method you can use this utility Class to sort entries as they are added to the map:

public class ValueSortedMap<K extends Comparable<K> ,V extends Comparable<V> > extends TreeMap<K,V> {

        private TreeMap<K,V> sortedMap;

        private ValueComparator comparator;

        private boolean reverseOrder = false;

        public ValueSortedMap() {
                this.comparator = new ValueComparator();
                this.sortedMap = new TreeMap<K, V>(comparator);
        }




        public ValueSortedMap(boolean reverseOrder) {
                this();
                this.reverseOrder = reverseOrder;


        }

        @Override
        public String toString() {
                return sortedMap.toString();
        }

        @Override
        public V put(K key, V value) {
        if(sortedMap.containsKey(key)){
            //remove the key in the sorted set before adding the key again
            remove(key);
        }               
                comparator.unsortedMap.put(key, value);
                return sortedMap.put(key, value);
        }


        @Override
        public Map.Entry<K, V> firstEntry() {
                return sortedMap.firstEntry();
        }       



        @Override
        public void clear() {
                sortedMap.clear();
                comparator.unsortedMap.clear();
        }

        @Override
        public boolean containsKey(Object key) {
                return sortedMap.containsKey(key);
        }

        @Override
        public boolean containsValue(Object value) {
                return sortedMap.containsValue(value);
        }


        @Override
        public Set<Map.Entry<K, V>> entrySet() {
                return sortedMap.entrySet();
        }

        @Override
        public V get(Object key) {
                return sortedMap.get(key);
        }


        /**
         * 0-based position
         * @param position
         * @return
         */
        public Map.Entry<K, V> getEntryInPosition(int position) {
                Iterator<Map.Entry<K,V>> iterator = sortedMap.entrySet().iterator();
                int i = 0;
                while(iterator.hasNext()){
                         if(i == position){
                                 return iterator.next(); 
                         }else{
                                 i++;
                         }
                }

                return null;

        }       

        @Override
        public boolean isEmpty() {
                return sortedMap.isEmpty();
        }



        @Override
        public Set<K> keySet() {
                return sortedMap.keySet();
        }



        @Override
        public void putAll(Map<? extends K, ? extends V> map) {
                for(Map.Entry<? extends K, ? extends V> entry : map.entrySet()){
                        put(entry.getKey(), entry.getValue());
                }
        }       



        @Override
        public int size() {
                return sortedMap.size();
        }

        @Override
        public Collection<V> values() {
                return sortedMap.values();
        }


        @Override
        public V remove(Object key) {
                sortedMap.remove(key);
                return comparator.unsortedMap.remove(key);
        }


        public class ValueComparator implements Comparator<K>{

                public Map<K,V> unsortedMap = new HashMap<K,V>();


                @Override
                public int compare(K k1, K k2) {
                        Comparable<V> v1 = unsortedMap.get(k1);
                        Comparable<V> v2 = unsortedMap.get(k2);
                        if(Objects.equal(v1,v2)){
                                return k1.compareTo(k2); //not using reverseOrder comparing keys
                        }else{
                                if(reverseOrder){
                                     return ComparisonChain.start().compare(v2, v1, Ordering.natural().nullsFirst()).result();
                                }else{
                                        return ComparisonChain.start().compare(v1, v2, Ordering.natural().nullsFirst()).result();
                                }
                        }
                }

        }       

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

Comments

0

If you would like to stick to using an array, you can sort the array storing the sales figures normally, but make the same changes to the names array.

For example, swapping the positions of 200 and 400 in the sort would be accompanied by swapping the positions of B and A in the other dimension of the array.

As others mentioned, using a map is probably the best way to go about this, but this also works if you would like to keep it a little simpler.

1 Comment

how will i do that?

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.