I need to sort out the following array-
“Blouse” “W” 21 17.95
“Blouse” “C” 35 17.95
“Shirt” “M” 13 21.95
“Pants” “W” 22 67.95
“Pants” “M” 22 62.95
“Pants” “C” 26 21.95
“Coat” “C” 20 21.95
“Coat” “M” 10 62.95
“Blouse” “W” 21 17.95
“Blouse” “C” 35 17.95
“Shirt” “M” 13 21.95
“Pants” “W” 22 67.95
“Pants” “M” 22 62.95
“Pants” “C” 26 21.95
“Coat” “C” 20 21.95
“Coat” “M” 10 62.95
If the prices are same, I need to give priority to C before W, and W before M. I need to use selection sorting for this program, and it's as follows-
public static void selectionSort (Comparable[] list, int n)
{
int min;
Comparable temp;
for (int i = 0; i< n - 1;i++)
{
min = i;
for(int scan = i + 1; scan < n; scan++)
{
if (list[i].compareTo(list[scan]) < 0)
min = scan;
}
temp = list[min];
list[min] = list[i];
list[i] = temp;
}
I am having problems when the prices are equal, my compareTo method is as follows. I am thinking of using sentinel values to give priority to these categories, but how would I do that? Also, what should the sorting algorithm do, having all these sentinel values for different categories while going through the whole array. How should it pick the proper (C before W, w before M) object to swap?
public int compareTo(Object obj)
{
int result;
double objPrice = ((Item)obj).getPrice();
char objCategory = ((Item)obj).getCategory().charAt(1);
if (objPrice == price)
// {
// if(objCategory.charAt(1) == 'M')
// return result = 0;
// else
// return result = 1;
// }
switch (objCategory)
{
case 'C':
return -1;
}
else
return result = (int)(objPrice - price);
}