The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Patterns for data sorting/grouping in memory

Hi all,

are there any general patterns about sorting and grouping of data in memory?

My application should read statistic results from a poultry scale. The poultry scale is used to weigh live chicks during they grow and the main reason to use the scale is to get an average weight of a flock during the growing period. There can be more scales used at one farm and after the results are downloaded to the PC, I have a set of scales and a set of weighing results for each scale. For example:

SCALE_NAME    DATE          AVERAGE_WEIGHT
SCALE1        2008-08-01    2.56
SCALE1        2008-08-05    2.75
SCALE2        2008-08-01    2.32
SCALE2        2008-08-05    2.60

Once I load the data from a database, I need to sort and group it according to the scale name and time. For example, I need to display a graph of weight over time for one scale, compare this graph to another scale etc.

What I don't know is how the data will be organized in memory? Do I have the scale name stored directly in the Weighing class or do I make some list of scales first and then in each Weighing class I point it to the appropriate Scale object? I can imagine how to get the data from database, but I somehow don't know how to work similarly when the data is in memory. I use .NET 2.0, so LINQ is out of question.

As you can see, I don't even know what to ask or what to look for... Are there any general approaches to this? I believe this problem must be common.

Thank you,
Petr
Petr Veit
Tuesday, August 05, 2008
 
 
When you load the data from the database, where do you load it into? I assume you are loading that data into some sort of table or dataset. Can't you sort the data during the retrieval process i.e. in your SQL statement you can include an ORDER clause such as ORDER BY SCALE, DATE, WEIGHT. Hope this helps.
Phillip Flores Send private email
Tuesday, August 05, 2008
 
 
If you are using .NET, then best practice is to hold your collection of objects in a Generics list. You can now sort your list by any property by using the IComparer interface.

Here is some sample code to then sort your list:

using System;
using System.Collections.Generic;
using System.Reflection;

public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder { Ascending, Descending, None };
    private string sortColumn;
    private SortOrder sortingOrder;

    public GenericComparer(string sortColumn, SortOrder sortingOrder)
    {
        this.sortColumn = sortColumn;
        this.sortingOrder = sortingOrder;
    }

    #region public property

    /// <summary>
    /// Column Name(public property of the class) to be sorted.
    /// </summary>
    public string SortColumn
    {
        get { return sortColumn; }
    }

    /// <summary>
    /// Sorting order.
    /// </summary>
    public SortOrder SortingOrder
    {
        get { return sortingOrder; }
    }
    #endregion

    #region IComparer<T> Members
    public int Compare(T x, T y)
    {
        PropertyInfo propertyInfo = typeof(T).GetProperty(sortColumn);
        IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
        IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);


        if (obj1 == null && obj2 == null)
            return 0;
        else
            if (sortingOrder == SortOrder.Ascending)
            {
                if (obj1 == null && obj2 != null)
                    return -1;
                else if (obj1 != null && obj2 == null)
                    return 1;
                else
                    return (obj1.CompareTo(obj2));
            }

            else
            {
                if (obj1 == null && obj2 != null)
                    return 1;
                else if (obj1 != null && obj2 == null)
                    return -1;
                else
                    return (obj2.CompareTo(obj1));
            }


    }
    #endregion
}
Donald Lee Send private email
Tuesday, August 05, 2008
 
 
There are a lot of ways of sorting and grouping data.

One of the ways that non programmers use is the crosstabulating feature of many spreadsheets.  In MS Excel, this feature is called a "pivot table".

At first, the pivot table feature might look complex and mysterious.  But as you begin to use it,  it quickly becomes simple and intuitive.  Even if you are planning a programmatic solution to your problem,  you might want to spend a little time coming up to speed on the Excel pivot table, perhaps using some tutorial material. 

This could give you a good handle on how to think about the problem in your programming environment. 

There are lots of ways of getting the data into Excel from just about any database.
Walter Mitty Send private email
Tuesday, August 05, 2008
 
 
When I run across this, I usually create an in-memory sqlite database, load it with data, and perform sql operations. this works very well for me and has great performance since there's no disk i/o.

Best Regards,
Don
Don Dickinson Send private email
Wednesday, August 06, 2008
 
 
+ 1 for Phillip's "do it in the database". That's what they are designed to do.
David Aldridge Send private email
Wednesday, August 06, 2008
 
 
You need to do all this in SQL. Its designed to allow you to sort, group, sum, average, etc. LEarn T-SQL and you will find you can quickly run through many different scenarios where you group items say by weight, date, or type, then run averages and sums of that data. Once you build a couple queries in a stored procedure, you can then bolt those into your ASP>NET pags, or SQL Reporting Servics, Crystal Reports, or output to excel and do some graphing and charting that way.

Whenever you are talking about data, the best place to massage that data and begin to build Reports is in the database itself.
Ranger
Thursday, August 07, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz