Sorting Plist Values within a Specific Date Range.

Sorting plist by its value

Introduction

In this article, we will explore how to sort a plist (Property List) based on its values. A plist is a file that stores data in a human-readable format, commonly used for storing application settings or other configuration data.

The specific requirement here is to filter the plist so that only items within a certain date range (in this case, one week) are displayed. We will explore how to achieve this by modifying the existing plist reading and graph drawing code.

Understanding Plist Structure

A typical plist has the following structure:

{
    "key": {
        "subKey1": value,
        "subKey2": value,
        ...
    }
}

In our case, we have a plist with a single key-value pair, where the value is an array of dictionaries.

Reading Plist Data

To read the plist data, we use the dictionaryWithContentsOfFile: method:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

This method returns a dictionary containing all the key-value pairs in the plist file.

Sorting Plist Values

The existing readFromPlist method simply returns the value of the “title” key without any filtering:

NSArray *valueArray = [dict objectForKey:@"title"];

To sort the plist values, we need to filter the data based on a specific date range. We will use the readFromPlistForOneWeek method to achieve this.

Filtering Plist Data

The readFromPlistForOneWeek method filters the plist data by looping through each item and checking if it is within the desired date range:

NSMutableArray *tempValueArray=[NSMutableArray new];
for (NSDictionary *subDict in [dict objectForKey:@"title"]) {
    // ...
}

Here, we assume that the “date” key in each dictionary represents a timestamp.

Converting Date Strings to Date Objects

To filter the dates, we need to convert the string representations of the dates to NSDate objects:

NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"dd-MMM-yy"];

NSDate *plistDate=[dateFormatter dateFromString:plistDateString];

// ...

This method uses an NSDateFormatter to parse the date string and create a corresponding NSDate object.

Calculating Date Differences

To determine if the plist date is within one week of the current date, we calculate the difference in seconds between the two dates:

NSTimeInterval secondsBetween = [plistDate timeIntervalSinceDate:currentDate];

We then divide this value by 86400 (the number of seconds in a day) to get an integer representing the number of days since the plist date.

Filtering Plist Values

Finally, we filter the plist values based on the calculated date differences:

if(dateDiff<8){ //within 0-7 days
    [tempValueArray addObject:subDict];
}

This method adds the dictionary to the tempValueArray if it is within one week of the current date.

Drawing Graph with Sorted Plist Data

To draw the graph using the filtered plist data, we modify the existing code:

NSMutableArray *Array=[NSMutableArray arrayWithArray:[self readFromPlistForOneWeek]];

// ...

for (id object in [Array reverseObjectEnumerator]){
    // ...
}

We use the readFromPlistForOneWeek method to get the filtered plist data and then draw the graph using this array.

Conclusion

In this article, we explored how to sort a plist based on its values by modifying the existing plist reading and graph drawing code. We covered the basics of plist structure, reading and filtering plist data, and drawing graphs with sorted plist data. By following these steps, you can achieve similar results in your own applications.

Code Examples

Here are the modified code examples:

// Modified readFromPlistForOneWeek method
- (NSArray *)readFromPlistForOneWeek {
    // ...

    NSMutableArray *tempValueArray=[NSMutableArray new];
    for (NSDictionary *subDict in [dict objectForKey:@"title"]) {
        // ...

        NSDateFormatter *dateFormatter=[NSDateFormatter new];
        [dateFormatter setDateFormat:@"dd-MMM-yy"];

        NSDate *plistDate=[dateFormatter dateFromString:plistDateString];

        NSTimeInterval secondsBetween = [plistDate timeIntervalSinceDate:currentDate];

        NSInteger dateDiff = secondsBetween / 86400;

        if(dateDiff<8){ //within 0-7 days
            [tempValueArray addObject:subDict];
        }
    }

    return tempValueArray;
}

// Modified graph drawing code
for (id object in [Array reverseObjectEnumerator]){
    // ...
}

Last modified on 2025-03-25