Skip to content

Commit

Permalink
Fixed sort to produce new list
Browse files Browse the repository at this point in the history
  • Loading branch information
pavly-gerges committed Jul 2, 2021
1 parent 9fc2781 commit 5a901f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,13 @@ public String[] search(String[] searchList, String[] searchKeyWords, ActionInjec
synchronized(this) {
final String[] resultList = new String[searchList.length];
return Executors.callable(() -> {
//format the list
removeAllViews();
for (int pos = 0; pos < searchList.length; pos++) {
for (String keyword : searchKeyWords) {
if (searchList[pos].replaceAll(" ","").trim().toLowerCase().contains(keyword.replaceAll(" ","").trim().toLowerCase())) {
resultList[pos] = searchList[pos];
View uiState = getChildUiStateByIndex(pos);
if(injector != null){
injector.execute(uiState, pos);
injector.execute(getChildUiStateByIndex(pos), pos);
}
addView(uiState);
break;
}
}
Expand Down Expand Up @@ -240,43 +236,45 @@ public void revertSearchEngine(@Nullable ActionInjector actionInjector) throws E
*/
public String[] sort(String[] list, int sortAlgorithm) throws Exception {
synchronized(this) {
//to apply the sort change as an external final change on a list copy (warning : ->Internal List change(positions or items count or items values) = Malicious Activity
final String[] copy = list;
return Executors.callable(() -> {
String tempPointer = "";
//main String List looping
for (int i = 0; i < list.length; i++) {
for (int i = 0; i < copy.length; i++) {
//looping over the String again to compare each one String member var with the sequence of the String member vars after that item
for(int j = i+1; j < list.length; j++ ){
for(int j = i+1; j < copy.length; j++ ){
//sort from A-Z ascendingly
if(sortAlgorithm == A_Z){
//compare 2 strings lexicographically based on their characters, if the (string object > the argument string) then compareTo returns 1
if ( list[i].toLowerCase().compareTo(list[j].toLowerCase()) > 0 ){
if ( copy[i].toLowerCase().compareTo(copy[j].toLowerCase()) > 0 ){
//format the pointer
tempPointer = "";
//then swap list[i] & list[j] because list[i] is after the list[k]
//store the list[i] inside the tempPointer for later access
tempPointer = list[i];
tempPointer = copy[i];
//get the list[i] after
list[i] = list[j];
copy[i] = copy[j];
//get the list[j] before
list[j] = tempPointer;
copy[j] = tempPointer;
}
}else if(sortAlgorithm == Z_A){
//compare 2 strings lexicographically based on their characters, if the (string object < the argument string) then compareTo returns -1
if ( list[i].toLowerCase().compareTo(list[j].toLowerCase()) < 0){
if ( copy[i].toLowerCase().compareTo(copy[j].toLowerCase()) < 0){
//format the pointer
tempPointer = "";
//then swap list[i] & list[j] because list[i] is before the list[k]
//store the list[j] inside the tempPointer for later access
tempPointer = list[j];
tempPointer = copy[j];
//get the list[j] before
list[j] = list[i];
copy[j] = copy[i];
//get the list[i] after
list[i] = tempPointer;
copy[i] = tempPointer;
}
}
}
}
}, list).call();
}, copy).call();
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void testPagerUiStates() throws Exception {
//attach the button
uiPager.attachUiState(button, UiPager.SEQUENTIAL_ADD);
}
System.out.println(Arrays.toString(uiPager.sort(new String[]{"199", "110", "990", "99", "222", "333", "4455",}, UiPager.A_Z)));
System.out.println(Arrays.toString(uiPager.sort(new String[]{ "0?Dogy", "=Baka", "9ggBi", "D_aD", "dad", "amam", "-lolo", "\"hi", "Come", "come", "C", "F", "I", "Z", "A"}, UiPager.A_Z)));
// System.out.println(Arrays.toString(uiPager.sort(new String[]{"199", "110", "990", "99", "222", "333", "4455",}, UiPager.A_Z)));
// System.out.println(Arrays.toString(uiPager.sort(new String[]{ "0?Dogy", "=Baka", "9ggBi", "D_aD", "dad", "amam", "-lolo", "\"hi", "Come", "come", "C", "F", "I", "Z", "A"}, UiPager.A_Z)));

}

Expand Down Expand Up @@ -90,5 +90,25 @@ public void onClick(View v) {
}).start();
}
}
private class DataModel {
private int id;
private String text;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}
}

0 comments on commit 5a901f2

Please sign in to comment.