Alphabetically sorting an ArrayList<String> using .compareTo() - java

An assignment we have to do in school is alphabetically sort two different ArrayLists into a final arraylist that contains both of the lists elements lexicographically
For example...
ArrayList sent1 contains in alphabetical order [., adam, mark]
ArrayList sent2 contains in alphabetical order [., betsy, kyle, william]
ArrayList sent3 must contain all of the elements in both ArrayLists in alphabetical order.
I just can't seem to figure out how to do it. My instructor mentioned using a while loop, but I don't just understand how to code the process. Combining sent1 and sent2 into sent3 and then using Collections.sort(sent3) would make the process so much easier and more compact, but the goal of the lab is to practice the using of compareTo().

If each source list is already sorted, just look at the first element in each list, add the "lower" (alphabetically first) to the output list, and repeat.

It's pretty simple;
List<String> sent3 = new ArrayList<String>(sent1);
sent3.addAll(sent2);
Collections.sort(sent3, new Comparator<String>() {
public int compare(String a, String b) {
return a.compareTo(b); // usage here!
}
}

What you are looking for is to use 2 iterators and just merge these 2 lists.
I'll give you some pseudo code.
ArrayList sent1, sent2, sent3;
int i1, i2;
i1 = i2 = 0;
while (i1 < sent1.length() && i2 < sent2.length())
{
if (sent1[i2].compareTo(sent2[i1])
{
sent3.add(sent1[i2]);
i2++;
}else
{
sent3.add(sent1[i1]);
i21+;
}
}
while(i1 < sent1.length()) sent3.add(sent1[i1++]);
while(i2 < sent2.length()) sent3.add(sent2[i2++]);
Here each time will add to my sent3 the minimum of the first items in sent1 and sent2. And when I get one of this lists empty, I will just append the rest of other elements into the sent3.

I think that you can do this:
copy array1 and array2 to array3; then, sort array3. you don't know how to sort using compareTo() ? Check is java documentation.

Related

sorting 2d array using build in sort function

I was using Arrays.sort() function to sort 2d array (int[][] array). Since I want to sort it base on the first element. For example, {{2,3},{1,4}} base on 1st element the array will be {{1,4},{2,3}}. So I override the compare function.
Arrays.sort(arr, new Comparator<int[]>() {
#Override
public int compare(int[] o1, int[] o2){
if(o1[0] < o2[0]){
return -1;
} else if (o1[0] > o2[0]) {
return 1;
} else {
return 0;
}
}
})
I know this sort work. But I don't understand how this compare work.
I was thinking the
new Comparator<int[]>
should be
new Comparator<int[][]>
since this is 2d array. and inside of compare function should be compare
o1[0][0] and o2[0][0]
Can anyone help me understand it?
Also this is using Arrays.sort, can I use Collections.sort? what is different between it?
Thanks
Remember that a "2D array" doesn't actually exist in Java, so what you're really dealing with is "an array of int[]" (there's nothing inherently preventing each of those int[] from being a different length).
So: when you sort, you're comparing individual elements of that "array of int[]" with each other, and because each element is an int[], your Comparator is for int[], too.
You are passing the array in the sort method and giving it a comparator. Arrays.sort will use iterator to pass into compare method. So compare method is checking element at arr[0].compare(arr[1]) sorts these 2 and goes to next iterator. same concept applies using 2d array. You are just passing 2 arrays and telling compare who should be placed where.

Java Using Comparators in combination with custom Comparators

I want to sort the following example list which currently contains only Strings with my own custom rules.
ArrayList<String> coll = new ArrayList<>();
coll.add("just");
coll.add("sdsd");
coll.add("asb");
coll.add("b as");
coll.add("just");
coll.add("dhfga");
coll.add("jusht");
coll.add("ktsa");
coll.add("just");
coll.add("just");
I know that I could write my own comparator for this, but as I know that Java also got comparators which solve this problem partially I want to know how I can use the ones from the Java API in combination with my own one.
How should it be sorted?
The word just should always be the first word to appear in the list followed by all other words in alphabetical order.
Comparator.naturalOrder() sorts the list in alphabetical order, but how can I combine this comperator with a custom one which checks whether the word is just or something else.
You can do this something like that:
coll.sort(Comparator
.comparingInt((String s) -> s.equals("just") ? 0 : 1) // Words "just" first
.thenComparing(Comparator.naturalOrder())); // Then others
You could integrate the criteria into the comparator like
coll.sort(Comparator.comparing((String s) -> !s.equals("just"))
.thenComparing(Comparator.naturalOrder()));
or you separate the operations, first moving all occurrences of "just" to the front, then sorting the remaining elements only:
int howManyJust = 0;
for(int ix = 0, num = coll.size(); ix < num; ix++)
if(coll.get(ix).equals("just") && ++howManyJust <= ix)
Collections.swap(coll, ix, howManyJust-1);
coll.subList(howManyJust, coll.size()).sort(Comparator.naturalOrder());
while this may look more complicated, it is potentially more efficient, especially for larger lists.
The first step should be to define the custom order. I would do that by using a Map.
Map<String, Integer> orderMap = new HashMap<>();
int order = 0;
for(String specialWord : yourListOfSpecialWords){
orderMap.put(specialWord, order++);
}
Now build comparator using that map and natural order as backup:
Comparator<String> comparator = ((Comparator<String>) (o1, o2) -> {
int leftScore = orderMap.getOrDefault(o1, Integer.MAX_VALUE);
int rightScore = orderMap.getOrDefault(o2, Integer.MAX_VALUE);
return Integer.compare(leftScore, rightScore);
}).thenComparing(String::compareTo);
Use this comparator to sort your list. Note: you probably want to initialize your map only once and keep it in a constant or at least in a cache.
But if your special case is only a single word, as your update suggests, then this is of course overkill, and you should go with one of the other answers here.

how to merge two sorted arrays of same size?

1st array: {3,5,6,9,12,14,18,20,25,28}
2nd array: {30,32,34,36,38,40,42,44,46,48}
Sample Output:
{3,5,6,9,12,14,18,20,25,28,30,32,34,36,38,40,42,44,46,48}
I have to merge 1st array into second array. 2nd array has space to accomadate all values
Whenever i know i need a collection to be sorted, i use a method that will insert new elements in right place, so the collection will never have state when its not sorted... in your case you might be good with adding two into destination collection then use Collections.sort() but you can do sortedInsert() as well... you can create your collection and start addin all items into it using this method, and when you finished you dont need another call to Collections.sort() because collection is always in sorted state... This is handy if you often do single element update and dont want whole collection to be resorted... this will work with much better performance...
Here is what i do for List
/**
* Inserts the value keeping collection sorted, provided collections shall be sorted with provided
* comparator
*/
public static <E> void sortedInsert(List<E> list, E value, Comparator<? super E> comparator) {
assert Ordering.from(comparator).isOrdered(list);
if (list.size() == 0) {
list.add(value);
} else if (comparator.compare(list.get(0), value) > 0) {
list.add(0, value);
} else if (comparator.compare(list.get(list.size() - 1), value) < 0) {
list.add(list.size(), value);
} else {
int i = 0;
while (comparator.compare(list.get(i), value) < 0) {
i++;
}
list.add(i, value);
}
}
Use System.arraycopy to append a1 to a2
System.arraycopy(a1, 0, a2, a2_len, a1.length);
then Arrays.sort
Use two pointers/counters, i & j starting from 0 to size of the array. Compare a[i] & b[j] and based on the result shift i or j (similar to merge sort, merging step). If extra space isn't allowed then in worst case (which is true in your input, all the elements in first array is smaller than first element in second array) you might have to shift 2nd array every time you compare elements.
Make a ArrayList object as arrayListObject
ArrayList<Integer> arrayListObject= new ArrayList<>();
Add elements of both arrays in that arrayListObject
Do Collectios.sort(arrayListObject) to sort the elements.
Use
Integer list2[] = new Integer[arrayListObject.size()];
list2 = arrayListObject.toArray(list2);
to get the resulted array

Common elements of muiltiple arrays but not using Lists

Is there a way i could return in an array the common elements of 2 or more arrays? I know having some of the methods under lists could do it but is there a way to do it by only using arrays? I made my own get and length btw since i am creating a an array called OrderedIntList.
Example would be:
1,3,5
1,6,7,9,3
1,3,10,11
Result: 1,3
I tried this and it outputs the common elements between two arrays and not all.
I know there's something wrong but i do not how to make it work like it suppose to work :(
//returns the common elements of inputted arrays
public static OrderedIntList common(OrderedIntList ... lists){
int[] list = new int[10];
for(int x = 1; x <= lists.length -1; x++){
for(int q = 0; q < lists[0].length()-1; q++) {
for(int z = 0; z < lists[x].length(); z++) {
if (lists[0].get(q)==lists[x].get(z)){
list[q] = lists[0].get(q);
}
}
}
}
OrderedIntList newlist = new OrderedIntList(list);
return newlist;
}
This can be an easy algorithm to solve it...
1) Instantiate an instance variable of type array called
"commonElements" pointing to the elements of the first Array. At the
beginning these are your common elements.
2) Create a method call getCommonElements(int[] commonElements,
int[] newList). This method manipulates the commonElements array to leave
it with only the common elements between the two. (p.s Use a temporary
array to achieve this if you find it easier)
3) Iterate over all the arrays present in "lists" starting from the
second array.
4) call the method at point 2 for each array .
All the difficult part for you it's to implement a method that given 2 arrays finds the common elements!
You can use
org.apache.commons.collections.CollectionUtils.intersection(java.util.Collection a, java.util.Collection b)
to get the intersection of two lists (elements presents in both lists)
And to pass your array as a Collection: java.util.Arrays.asList(Object[] a);
But working on arrays is tedious, at best. You should consider why you don't want to use a Collection...
As a partial answer, you're probably doing too much work by fully reimplementing an OrderedIntList the way you're doing, since ArrayList and friends already come with sorting baked in via the Collections class.
import java.util.Collections;
public class OrderedIntList extends ArrayList<Integer> {
#override // to effect sorted inserts
public void add(Integer i) {
this.add(i);
Collections.sort(this);
// done.
}
}
Wanting to do this for pure arrays is a nice exercise, but then you'll be better of implementing sorting properly with a quick sort (you can't websearch for a java implementation of that without getting a million results) or an insert sort (equally websearchable), and follow the same recipe.
any time you push a number into the array:
guess where the number goes (although that's optional),
insert the number,
resort your array if you know your guess wasn't perfect.

Comparing and sorting in java

I have 2 arrays of data in Java. Based on the order of first array I have to sort the next array.
E.g -
String[] Array1 = {"EUROPE","MIDDLEEAST","OTHERs","AUSTRALIA"};
String[] Array2 = {"MIDDLEEAST","EUROPE","AUSTRALIA","OTHERs","ASIA","EUROPE"};
My output should look like:
{"EUROPE","EUROPE","MIDDLEEAST","OTHERs","AUSTRALIA","ASIA"}
What is the best way to do it?
To sort you need to define a sorting order so given element A and B, you can determine easily if A should go before or after B in the sorted list.
This concept is formalized with the concept of a Comparator in Java.
In this case the sorting order is defined by the order of the elements in a list. The simplest approach is given A and B to find each of them in the original list, note the index found, and compare the indexes to find out which one goes first.
Depending on the size of your data this might be too slow. You can then create a HashMap<String,Long> which holds the index of a given string in Array1. Here it would hold "DEF"->0, "ABC"->1, "XYZ"->2.
May be this:
1) Sort both of them.
2) Create blank result table
3) Take first elem from sorted Array2 and put it to result table with the original index of the first elem on sorted Array1
3) Repeat the step 3 on the second element and so on.
The computational complexity would be like sorting method used: O(nlogn) for quicksort
You can definitely do this in O(n log n). But the best approach depends on what is more important: quick, clean code or not allocating extra memory.
If you don't care about using extra memory, you can allocate a separate array, where each element is a pair:
public class Pair implements Comparable {
...
}
Then you would sort the array of pairs using Arrays.sort(Object[]).
If you don't want to allocate quite so much space, you can use an auxiliary array that contains the indexes in Integer form:
final String[] array1 = ...;
final String[] array2 = ...;
assert array1.length == array2.length;
Comparator<Integer> c = new Comparator<Integer> {
int compare(Integer a, Integer b) {
return array1[a].compareTo(array1[b]);
}
};
Integer[] aux = new Integer[array1.length];
for (int i = 0; i < aux.length; ++i) { aux[i] = i; }
Arrays.sort(aux, c);
String[] result = new String[array1.length];
for (int i = 0; i < aux.length; ++i) {
result[i] = array2[aux[i]];
}
If you are trying to do the entire thing in-place and not allocate additional memory, then you will need to implement one of the n-log-n sort algorithms yourself...
There are (at least) two ways to sort one array and reorder a second array so corresponding elements still match. Both require constructing a third array and writing a custom comparison function.
Method 1
Define a custom object that contains one element of each array. (In your case, it might be a two-element String array.) Write a comparator (or implement Comparable) for the custom object that simply compares the elements from the first array. Build an array of the custom objects from the two input arrays, sort the third array, and then extract the results.
This is the method most commonly recommended for this problem.
Method 2
Construct an array of Integer indexes initialized to 0, 1, 2, ..., n-1 (where n == Array1.length). Sort the index array using a comparator that compares indexes by comparing the Array1 elements that they index.
The second method will be faster and will not require as much object construction.
Two other ideas:
Could these values be an enum rather than a set of strings? The natural order of an enum is the order of declaration, so Arrays.sort() would just work.
Helper code exists in Guava's Ordering.explicitOrder(List):
String[] explicitOrder = {"EUROPE", "MIDDLEEAST", "OTHERs", "AUSTRALIA"};
String[] toSort = ...
Comparator<String> comparator = Ordering.explicit(Arrays.asList(explicitOrder));
String[] sorted = Arrays.sort(toSort, comparator);

Categories