logo
down
shadow

Arranging Ten Inputted Numbers into Ascending and Descending order


Arranging Ten Inputted Numbers into Ascending and Descending order

Content Index :

Arranging Ten Inputted Numbers into Ascending and Descending order
Tag : chash , By : user109285
Date : November 27 2020, 09:01 AM

Does that help I want to create a program that lets me enter ten different numbers in ten different textboxes, then later on sort these inputted numbers into ascending OR descending order. The windows form application design has ten textboxes in it, a button named "Sort" (the button that will sort out the numbers depending on the radio button of your choice) and two radio buttons named "Ascending" and "Descending". The design also have ten labels, wherein the sorted out numbers will be displayed after the user clicked on the Sort button. However, I'm finding troubles regarding the actual coding itself, I considered using a nested for loop but couldn't handle the conditions. Now I've used an array supported with multiple if else statements. , Try this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<TextBox> textBoxes = null;
        List<Label> labels = null;
        public Form1()
        {
            InitializeComponent();

            textBoxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10 };
            labels = new List<Label>() { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10 };

            for (int i = 0; i < textBoxes.Count; i++)
            {
                labels[i].Text = textBoxes[i].Text;
            }
            for (int i = 0; i < labels.Count - 1; i++)
            {
                int iInt = int.Parse(labels[i].Text);
                for (int j = i + 1; j < labels.Count; j++)
                {
                    int jInt = int.Parse(labels[j].Text);
                    if (jInt < iInt)
                    {
                        iInt = int.Parse(labels[j].Text);
                        string temp = labels[i].Text;
                        labels[i].Text = labels[j].Text;
                        labels[j].Text = temp;
                    }
                }
            }
        }
    }
}
​
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<TextBox> textBoxes = null;
        List<int> numbers = new List<int>();
        List<Label> labels = null;
        public Form1()
        {
            InitializeComponent();

            textBoxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10 };
            labels = new List<Label>() { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10 };

            for (int i = 0; i < textBoxes.Count; i++)
            {
                 numbers.Add(int.Parse(textBoxes[i].Text));
            }
            numbers = numbers.OrderBy(x => x).ToList();
            for (int i = 0; i < numbers.Count; i++)
            {
                labels[i].Text = numbers.ToString();
            }
        }
    }
}
​

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

arranging numbers in ascending order


Tag : c , By : kuba53280
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


Tag : java , By : ranja
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


Tag : java , By : Hunter
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


Tag : php , By : msugar
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]);
}
Related Posts Related QUESTIONS :
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • "The attribute names could not be inferred from bind attribute 'bind-value'" exception in Blazor
  • How to fix ''System.ArgumentException" in c#?
  • C#. Ref returning delegate for ref extension method
  • Swashbuckle Swagger generate an actual guid
  • Trying to make thousands of database calls simultaneously - they stack instead of running concurently
  • run mstest from cmd using testcontainer
  • How to make M:N (many-to-many) relationship where both M and N are the same entities?
  • Consume COM DLL from multiple clients
  • Convert json string response representing UCHAR array to Byte array
  • Why does this LinqPad program produce different results on the second run?
  • ServiceStack how to use MaxLoginAttempts feature
  • What does "late-bound access to the destination object" mean?
  • How to load appsettings.json inside a Static class based on deploy environment so that I can use in other class librarie
  • dynamically add images to view
  • Are static extension methods on User object safe?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com