# Implementing Sorting in Arrays Question

• 10-10-2011, 07:43 PM
jakaldama
Implementing Sorting in Arrays Question
I am attempting to sort a number of Sales Persons' sales in an array. I first made a SalesPerson class that get name and sales #'s.

I then made another class named ArraySorter that should hopefully take the sales numbers and sort them in order.

I finally have the SalesReporter class file that consists of getData (enter in the number of sales people that will be used, the names and sales #'s)

I then need a section dealing with taking those numbers and implementing the arraySorter I have, I believe. I am taking bits and pieces of reading and examples from my textbook, which in a previous part may have created something like computeData (to get the average of the sales or something easier).

Then display results, and the main method which my thoughts are going towards:

SalesReporter clerk = new SalesReporter();
clerk.getData();
clerk.??? --this could have been computeData(); but I have a separate array-sorting class.
clerk.displayResults();

Am i looking at this the wrong way? I suppose I'm not understanding how to use my array sorter in this case.
• 10-10-2011, 08:36 PM
Iron Lion
Re: Implementing Sorting in Arrays Question
Without knowing how your array-sorting class works, it's hard to say.

You might be interested in the Arrays class though.
• 10-10-2011, 08:49 PM
jakaldama
Re: Implementing Sorting in Arrays Question
My Array sorting class is as follows:

public class ArraySorter
{
//every element in SalesAssociate has a value

public static void selectionSort(int[] sales)
{
for (int index = 0; index < sales.length - 1; index++)
{
int indexOfNextSmallest = getIndexOfSmallest(index, sales);
interchange(index, indexOfNextSmallest, sales);
}
}
private static int getIndexOfSmallest(int startIndex, int[] a)
{
int min = a[startIndex];
int indexOfMin = startIndex;
for (int index = startIndex + 1; index < a.length; index++)
{
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
}
}
return indexOfMin;
}

private static void interchange(int i, int j, int[] a)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

}