I want to sort .properties file in alphabetical order using php

72 Views Asked by At

**I want to sort .properties file in alphabetical order with comments. Only alphabetical order is working fine but when i am trying to sort with comments it's not showing comments.

this is my .properties file**

product.TRDMCL.shortdescription.listitem3=Rubber clip adapts to collar
cms.tracker-comparison.seo.meta-title=Compare the Features of Alpha GPS Trackers
# https://alpha.com/en/c/shipping-costs
#
# lh-check { max: 60 }
cms.shipping-costs.seo.meta-title=Shipping Costs and Delivery for Alpha GPS Trackers
jobs.open-positions.heading=Your career at Alpha
team.section2-4.title=Marketing
general.action.add-to-cart=Add to Cart
# Button that links to https://my.alpha.com/#/activate/device
navigation.header.activateTracker=Activate Tracker

I want to sort this file in alphabetical order including comments.

    $content = file_get_contents('short_codes.properties');
    $config_list = explode(PHP_EOL, $content);
    sort($config_list);
    $content="";
    foreach ($config_list as $config) {

        $content.=$config.'<br>';
                
    }
    echo ($content);

I am getting;


#
# Button that links to https://my.alpha.com/#/activate/device
# https://alpha.com/en/c/shipping-costs
# lh-check { max: 60 }
cms.shipping-costs.seo.meta-title=Shipping Costs and Delivery for Alpha GPS Trackers
cms.tracker-comparison.seo.meta-title=Compare the Features of Alpha GPS Trackers
general.action.add-to-cart=Add to Cart
jobs.open-positions.heading=Your career at Alpha
navigation.header.activateTracker=Activate Tracker
product.TRDMCL.shortdescription.listitem3=Rubber clip adapts to collar
team.section2-4.title=Marketing

But i am expecting

# https://alpha.com/en/c/shipping-costs
#
# lh-check { max: 60 }
cms.shipping-costs.seo.meta-title=Shipping Costs and Delivery for Alpha GPS Trackers
cms.tracker-comparison.seo.meta-title=Compare the Features of Alpha GPS Trackers
general.action.add-to-cart=Add to Cart
jobs.open-positions.heading=Your career at Alpha
# Button that links to https://my.alpha.com/#/activate/device
navigation.header.activateTracker=Activate Tracker
product.TRDMCL.shortdescription.listitem3=Rubber clip adapts to collar
team.section2-4.title=Marketing

1

There are 1 best solutions below

0
O. Jones On

In general, setting up a program for sorting involves doing two steps.

  1. You will create an array containing one element for each objects you need sorted. In your case, some of your objects are .properties lines and others are multiline blocks of comments.

    You will need to figure out how to create those multiline comment objects as you read your file.

  2. You will create a function that compares arbitrary pairs of those objects. The function returns a negative integer if the first object in the pair comes before the second one in your desired sort order. It returns a positive integer if the second one comes first, and zero if they should sort as equal. This function defines how you want your objects sorted. Like this:

     function ateeb_sort ( $a, $b ) {
       if ($a /* is before */ $b) return -1;
       if ($a /* is after  */ $b) return 1;
       return 0;
     }

Then call sort( $array, 'ateeb_sort' ); and you will get your array in your desired order.

Unfortunately your question doesn't give us enough exact information about either step for us to help you further.