Skip to main content

How to arrange wrong order the list data in Dart

List In Dart

1: => To replace the smallest number in a Dart list and move it to the first position, you can use the following code:-

void main() {

  List<int> a = [3, 5, 2, 8, 6, 10, 9];


  // Find the index of the smallest number in the list

  int minIndex = 0;

  for (int i = 1; i < a.length; i++) {

    if (a[i] < a[minIndex]) {

      minIndex = i;

    }

  }


  // Swap the smallest number with the number at the first position

  int temp = a[0];

  a[0] = a[minIndex];

  a[minIndex] = temp;


  // Print the updated list

  print(a);

}


2: =>You can sort a Dart list in ascending and descending order without using a separate function by using the sort method and providing a custom comparison function.
 Here's an example:

void main() {
  List<int> a = [3, 5, 2, 8, 6, 10, 9];

  // Sort in ascending order
  a.sort((a, b) => a.compareTo(b));
  print('Ascending Order: $a');

  // Sort in descending order
  a.sort((a, b) => b.compareTo(a));
  print('Descending Order: $a');
}

3:=> If you want to arrange a list in ascending and descending order without using the sort function, you can implement your own sorting algorithm. One simple sorting algorithm is the Bubble Sort algorithm. 
Here's an example in Dart:

void bubbleSortAscending(List<int> list) {
  int n = list.length;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      if (list[j] > list[j + 1]) {
        // Swap elements if they are in the wrong order
        int temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
      }
    }
  }
}

void bubbleSortDescending(List<int> list) {
  int n = list.length;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      if (list[j] < list[j + 1]) {
        // Swap elements if they are in the wrong order
        int temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
      }
    }
  }
}

void main() {
  List<int> a = [3, 5, 2, 8, 6, 10, 9];

  // Sort in ascending order using Bubble Sort
  bubbleSortAscending(a);
  print('Ascending Order: $a');

  // Sort in descending order using Bubble Sort
  bubbleSortDescending(a);
  print('Descending Order: $a');
}

Describe:
This example uses the Bubble Sort algorithm to arrange the list in ascending and descending order. The bubbleSortAscending and bubbleSortDescending functions are used to perform the sorting. Keep in mind that Bubble Sort is not the most efficient sorting algorithm for large lists, but it's a simple one for educational purposes.


Thanks for reading the article...

Comments

Popular posts from this blog

How to write a code of Encode and Decode json data in Dart language

 Encode and decode JSON data in dart language import 'dart:convert'; void main() {   // Original data as a Dart map   Map<String, dynamic> originalData = {     'field1': 'value1',     'field2': 42,     'field3': true,   };   // Encode the Dart map to JSON string   String jsonString = jsonEncode(originalData);   print('Original JSON String:');   print(jsonString);   // Encode the JSON string to base64   String base64EncodedString = base64.encode(utf8.encode(jsonString));   print('\nBase64 Encoded String:');   print(base64EncodedString);   // Decode the base64 string to JSON string   String decodedJsonString = utf8.decode(base64.decode(base64EncodedString));   print('\nDecoded JSON String:');   print(decodedJsonString);   // Decode the JSON string to a Dart map   Map<String, dynamic> decodedData = jsonDecode(decodedJsonString);   print('\nDecoded Dart Ma...

About of Free Learning Tech Point

  Welcome to Free Learning Tech Point , where knowledge meets accessibility. Our platform is dedicated to providing high-quality educational resources and e-learning opportunities to learners around the world, completely free of charge. Our Mission: At Free Learning Tech Point, we believe that education is a fundamental right, and everyone should have access to valuable learning materials. Our mission is to break down barriers to education by offering a diverse range of courses, tutorials, and resources across various subjects and disciplines. What Sets Us Apart: - Free Access: Our commitment is to make learning accessible to all. No subscription fees, no hidden costs – just free, open access to knowledge.    - Quality Content: We curate and create content that is both engaging and informative. Whether you're a student, professional, or lifelong learner, our resources are designed to cater to various learning styles and levels. - Diverse Subjects: From tech and science...

Privacy Policy Of Free Learning Tech Point

  Thank you for visiting Free Learning Tech Point . You can use our website, services, and products with the awareness that this Privacy Policy describes how we gather, use, disclose, and protect your personal information. 1- Information We Collect: We may collect personal information that you provide directly to us, such as your name, email address, and any other information you choose to provide when using our Services. We may also collect non-personal information, such as aggregated data and usage patterns. 2- How We Use Your Information: We may use the information we collect for various purposes, including but not limited to: Providing and improving our Services. Responding to your inquiries and requests. Analyzing usage patterns and trends. Sending you updates, newsletters, and other communications. Personalizing your experience on our platform. 3- Cookies and Similar Technologies: We may use cookies and similar technologies to collect information about your interactions with...