logo
down
shadow

Sorting an array of strings using values alphabetically and considering the cases of values in PHP


Sorting an array of strings using values alphabetically and considering the cases of values in PHP

By : user2956991
Date : November 22 2020, 03:03 PM
Does that help I finally used usort() to sort the array. i have the following class with methods that constitute algorithm:
code :
class Alphabetizer extends AbstComposite{

    private $input = array();
    private $output = array();
function Alphabetizer( $input){
    $this->input = $input;
    $this->transform();
    $this->output();
}
    static function checkcase($string) {

        if( preg_match('/[A-Z]/', $string)){
            return 1;
        }elseif(preg_match('/[a-z]/', $string)){
            return 0;
        }else{
            return "stupid";
        }
    }
  static    function caseorder($a,$b){
       $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        //checking the case ordering of words
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
                if(strcasecmp($a[$i],$b[$i] == 0)){
                    if((Alphabetizer::checkcase($a[$i]) ==0) && Alphabetizer::checkcase($b[$i]) ==1){
                        return -1;
                    }elseif(Alphabetizer::checkcase($a[$i]==1) && Alphabetizer::checkcase($b[$i] ==0)){
                        return +1;
                    }else{
                        return 0;
                    }
                }return 0;



        }
    }
    //works well for alphabetical ordering of sentences
   static  function  letterorder($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
            if(strtolower($a[$i]) < strtolower($b[$i])){
                return -1;
            }elseif(strtolower($a[$i]) > strtolower($b[$i])){
                return +1;
            }else{
                return 0;
            }

        }

    }
   static  function alphaorder($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
            if(strtolower($a[$i]) > strtolower($b[$i])){
                return 1;
            }elseif(strtolower($a[$i]) < strtolower($b[$i])){
                return -1;
            }


        }
    }


   static  function comparator($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $return_value=0;
     if(($return_value=Alphabetizer::letterorder($a,$b))==0){
         if(($return_value=Alphabetizer::caseorder($a,$b) )== 0){
             if(($return_value=Alphabetizer::alphaorder($a,$b)) == 0){
               if(strlen($a) < strlen($b))  {
                 return 1;
               }else{
                   return 0;
               }
             }else{return$return_value;}
         }else{ return $return_value;}

     }else{
         return $return_value;
     }

    }


    public function transform(){
  usort($this->input,array(&$this,"Alphabetizer::comparator"));

    }
    public function output(){
        return array_unique($this->input);
    }

} 


Share : facebook icon twitter icon
Problems Alphabetically Sorting an Array of Strings

Problems Alphabetically Sorting an Array of Strings


By : user2824616
Date : March 29 2020, 07:55 AM
Hope that helps Before you light the torches, I am trying to teach myself C++. I feel like I've got a good grasp of it, but I gotta dive in to reach the bottom if you know what I mean. I am trying to debug a simple program I am trying to make to teach myself. This programs goal is to alphabetically sort an array of strings in print each one out. I probably should use pointers in here when passing the arrays to my length() function but I haven't gotten that far yet. There are so many errors that I am not sure where the error actually is. Please tell me it's something like a missing semi-colon. I am pretty sure that the if (itemsToSort[j] < firstInAZOrder) line won't work but again, I am trying to learn the language and I have to start somewhere. , First of all:
code :
std::string itemsToSort[] = {
    'bob', 'john', 'tyler', 'anthony', 'luke', 'eric'
};
std::string itemsToSort[] = {
    "bob", "john", "tyler", "anthony", "luke", "eric"
};
void output (int sortedItems) {
    for (int i = 0; ; i++) {
        std::cout << sortedItems[i] << std::endl;
    }
}
void output (int* intArray, int arraySize) {
    for (int i = 0; i < arraySize; ++i) {
        std::cout << intArray[i] << std::endl;
    }
}
Sorting array of c strings alphabetically

Sorting array of c strings alphabetically


By : Hdeux Nek
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a homework problem I'm having bit of a problem with, I'm asked to sort an array of C strings alphabetically using C++, sorting algo used must be bubble sort. What I've done so-far (replicated below) can sort the array but only based on the first alphabet. How do I further sort strings the strings with the same initial alphabet ? , change
if (sar[b][0] > sar[b+1][0])
Values are not alphabetically sorting

Values are not alphabetically sorting


By : user3332307
Date : March 29 2020, 07:55 AM
Any of those help When I put a break in the code after I've called filter_deals, I look at Sheet8 (the sheet I am pasting to) and see that the output I want is sorted in the correct order. However, when I don't put in a break and let the code run all the way through from beginning to end, the values do not sort as I would like them to. I would like for each range of data pasted to be sorted alphabetically. , I would look at this and the other blocks with the same issue:
code :
With Sheet8
    .Range("G3", .Range("G3").End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
    .Range("G3", .Range("G3").End(xlDown)).Sort Key1:=Range("G3"), Order1:=xlAscending, Header:=xlNo
End With
Key1:=Range("G3")
Sorting an array of strings alphabetically in scala

Sorting an array of strings alphabetically in scala


By : Sameer Girkar
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to sort an Array of Strings in alphabetical order using scala. The Strings are passed in an array when the method is called. The method must also return an array with the sorted Strings. How would I do this? , You just need to call array.sorted.
Sorting an array of strings alphabetically

Sorting an array of strings alphabetically


By : maurus
Date : March 29 2020, 07:55 AM
hope this fix your issue Since you are using Java 7 and streams are not available, you can:
use the simpler for syntax (assuming user.getTasks() returns a list of Task objects - replace Task with your class if not) loop through the strings, concatenating them with the separator
code :
ArrayList<String> test = new ArrayList<String>(); 
for (Task task : user.getTasks()) {
    test.add(task.getTask().toString());
}
Collections.sort(test);

// make a string of the values with a pipe separator
StringBuilder valuesToPrint = new StringBuilder();
for (int index = 0; index < test.size(); index++) {
    if (index > 0) {
        valuesToPrint.append(" | ");
    }
    valuesToPrint.append(test);
}

System.out.println(valuesToPrint.toString());
Related Posts Related Posts :
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • Common integer representation over variable casting?
  • PHP - Prevent client from tampering the ID of a form
  • unexpected T_PUBLIC error
  • Types of testcases in PHPUnit
  • keeping a LDAP user logged in with PHP session
  • Detect if browser supports WebP format? (server side)
  • real_escape_string not cleaning up entered text
  • E-commerce products filter
  • Why having more than one test method in a file using Laravel raises error?
  • PHP Escaped special characters to html
  • Basic prepared statement not INSERTING
  • PHP combobox not showing in HTML table
  • MySql PHP row request to variable
  • Join two tables with one table has multiple rows matching
  • Looping through users with Instagram API requests is REALLY slow
  • Unable to add 30 days to date within loop
  • Return from mysqli_query() into array
  • php rename() function returns "No such file or directory" error
  • form submit inside navigation menu
  • PHP: reasons to use GET and POST request at the same time?
  • Is break; required after die() php
  • little depth of session in php
  • Authentication Logon page not working using PHP and Oracle on Apache / Windows 2008
  • Magento reindexing error product flat data
  • Laravel 4: Running composer when deploying to Heroku
  • Long Polling or WebSockets
  • How to use mysqli_stmt_bind_param for integers, boolean, etc
  • Determining the content of a link
  • MYSQL and PHP update events?
  • PHP explode all square bracket
  • Transparent hexagon-masked collage using PHP GD
  • Plugins or text do NOT show up on custom wordpress template
  • inserting data into mysqli database
  • Two arguments to closure function laravel
  • UnClear image exported by ffmpeg from mp4 file
  • is the time of computer and localhost same?
  • change custom data- value after ajax post success
  • Can We Restrict PHP Variables to accept only certain type of values
  • csv file is inserting a blank row in the last of other rows into mysql table
  • How to convert special characters in xml using php?
  • Mcrypt are very confusing
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org