arranging numbers in ascending order
Date : March 29 2020, 07:55 AM
it fixes the issue This is quite basic program called selection sort. The wiki article is: Selection sort. a = number [i];
number [i] = number [j];
number [j] = a;
a = number [i];
number[i] = number [j];
number [j] = a;
|
arranging strings in ascending and descending order
Date : March 29 2020, 07:55 AM
wish of those help I would take a different approach entirely. Yours is very homegrown, and Java has stuff built in that can do this, most notably here, the Stream API and Comparators String quitString = "quit";
List<String> userInputList = new ArrayList<>();
try(Scanner scanner = new Scanner(System.in)){ // This is called a "try with resources"
System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input \"" + quitString + "\"." + System.lineSeparator());
String encore = scanner.nextLine();
while(!encore.equalsIgnoreCase(quitString)){
encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces.
System.out.println("Enter the string you want to put in your sequence of strings");
encore = scanner.nextLine();
if(encore != null && !encore.isEmpty() && !encore.equalsIgnoreCase(quitString)) {
userInputList.add(encore);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
List<String> ascending =
userInputList.stream()
.sorted((strA, strB) -> strA.length() - strB.length())
.collect(Collectors.toList());
List<String> descending =
userInputList.stream()
.sorted((strA, strB) -> strB.length() - strA.length())
.collect(Collectors.toList());
StringBuilder sbAscending = new StringBuilder();
sbAscending.append("Here are your strings in ascending order: ");
ascending.forEach(userInput -> {
sbAscending.append(System.lineSeparator() + userInput);
});
System.out.println(sbAscending.toString());
StringBuilder sbDescending = new StringBuilder();
sbDescending.append("Here are your strings in descending order: ");
descending.forEach(userInput -> {
sbDescending.append(System.lineSeparator() + userInput);
});
System.out.println(sbDescending.toString());
Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input "quit".
Start
Enter the string you want to put in your sequence of strings
test
Enter the string you want to put in your sequence of strings
test2
Enter the string you want to put in your sequence of strings
test23
Enter the string you want to put in your sequence of strings
test234
Enter the string you want to put in your sequence of strings
quit
Here are your strings in ascending order:
test
test2
test23
test234
Here are your strings in descending order:
test234
test23
test2
test
|
Arranging strings in ascending and descending order using array lists
Date : March 29 2020, 07:55 AM
hop of those help? I would suggest you to sort the list using Collections.sort(); and Collections.reverse(); Also, you don't need the else if (descending == null) since you already initialized descending. Your code will look something like, import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
String longest = "";
List<String> ascending = new ArrayList<String>();
List<String> descending = new ArrayList<String>();
int loop = 0;
Comparator<String> comparator = new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
}
String encore = "";
while(true){
loop++;
System.out.println("Enter the string you want to put in your sequence of strings");
encore = scanner.nextLine();
if (encore.equalsIgnoreCase("quit")) {
break;
}
encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces
ascending.add(encore);
descending.add(encore);
Collections.sort(ascending, comparator);
Collections.sort(descending, comparator);
Collections.reverse(descending);
}
for (String str: ascending) {
if (str.length() > longest.length()) {
longest = str;
}
}
if (ascending.size() > 0) {
System.out.println("Here are your strings in ascending order : " + ascending);
System.out.println("Here are your strings in descending order : " + descending);
System.out.println("Here is the longest string : " + longest);
} else {
System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
}
scanner.close();
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
String longest = "";
List < String > list = new ArrayList < > ();
int loop = 0;
String encore = "";
while(true){
loop++;
System.out.println("Enter the string you want to put in your sequence of strings");
encore = scanner.nextLine();
encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces
if (encore.equalsIgnoreCase("quit")) {
break;
}
list.add(encore);
}
for (String str: list) {
if (str.length() > longest.length()) {
longest = str;
}
}
if (list.size() > 0) {
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
System.out.println("Here are your strings in ascending order : " + list);
Collections.reverse(list);
System.out.println("Here are your strings in descending order : " + list);
System.out.println("Here is the longest string : " + longest);
} else {
System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
}
scanner.close();
}
}
|
Arranging numbers within the digits in descending order without using arrays
Tag : java , By : Zinovate
Date : March 29 2020, 07:55 AM
I hope this helps . Assuming that your number is not greater than Integer.MAX_VALUE you can use Redix Sort algorithm. It works fine with the complexity of O(n). Here, you can find the solution in following code, that doesn't use any array or lists and Strings. public static void main(String[] args) {
int number = 45322;
int sortedNumber = 0;
/*
* This loop checks for each digit starting from 9 to 0.
* In case of ascending order it should be 0 to 9.
*/
for (int i = 9; i >= 0; i--) {
int tmpNumber = number;
while (tmpNumber > 0) {
int digit = tmpNumber % 10;
// Check for the greatest digit in the given number
if (digit == i) {
sortedNumber *= 10;
sortedNumber += digit;
}
tmpNumber /= 10;
}
}
System.out.println(sortedNumber); // prints 54322.
}
|
Laravel- arranging records in ascending and descending order
Date : March 29 2020, 07:55 AM
this will help There are many ways you can achieve the sorting issue but as you have setup your page let me tell you the way you can sort records. Create another action which point to your settings.photos route public function settings_photos($property_id) {
$form = Input::get('submit');
$orderBy = $form == "Ascending" ? "asc" : "desc";
# Fetch Images in of Specific Property
$list_of_images = Image::where('property_id', $property_id)
# Order by Asc/Desc
->sortBy('id', $orderBy)->get();
return view('settings.photos', ['image_array' => $list_of_images]); }
https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
@csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form>
<form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
@csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Descending" class="settings-photos-header2 text-center"/>
</form>
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main, 'id' => $id]);
}
|