Search code examples
javadoublerankoperationutilization

Java Function to rank and evaluate variables


I've got 4 double's

double newmanUtil = newmanYard.oreTonnes / time();
double yandi1Util = yandiMine1.oreTonnes / time();
double yandi2Util = yandiMine2.oreTonnes / time();
double miningAreaCUtil = miningAreaC.oreTonnes / time();

and I need an operation ( and loop) to rank the resulting numbers in order from least to greatest as four integers...

int NYRank = 0;
int Y1Rank = 0;
int Y2Rank = 0;
int MACRank = 0;

I feel like this is something simple that someone knows how to do quickly...

Thanks in advance,


Solution

  • This will work:

    package com.sandbox;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Sandbox {
    
        public static void main(String[] args) {
            double newmanUtil = 1;
            double yandi1Util = 3;
            double yandi2Util = 2;
            double miningAreaCUtil = 4;
    
            List<Double> items = new ArrayList<Double>();
            items.add(newmanUtil);
            items.add(yandi1Util);
            items.add(yandi2Util);
            items.add(miningAreaCUtil);
    
            Collections.sort(items);
    
            System.out.println(items);
        }
    }
    

    I've replaced your variables with real values just to prove that it works. Here's the output:

    [1.0, 2.0, 3.0, 4.0]

    What I did was take the values and put it in a list, then I sorted the list. Java knows how to sort a list of Doubles.