Chapter 4. More Pantry usage
Prev |  | Next

## Chapter 4. More Pantry usage

Now that you have seen some practical examples of how to use Pantry, there will be even more information that you will find useful in this chapter.

## 4.1. Using the `--sort` option to sort foods within a report

As you have seen before, Pantry does not sort foods when it stores them. This allows Pantry to store and retrieve foods very quickly, but it also means that your foods come out in a big jumble:

Example 4.1. Pantry does not sort foods

    $ pantry --date "May 8" --print traits-blank diary
    Snacks, popcorn, oil-popped, unsalted
    Group: Snacks
    Date: May 8 Meal: Dinner
    2 oz (57g)

    McDONALD'S, Cheeseburger
    Group: Fast Foods
    Date: May 8 Meal: Dinner
    1 item (119g)

    Apples, raw, with skin
    Group: Fruits and Fruit Juices
    Date: May 8 Meal: Breakfast
    Refuse: 10 percent Core and stem
    1 large (3-1/4" dia) (223g)

    McDONALD'S, French Fries
    Group: Fast Foods
    Date: May 8 Meal: Dinner
    1 large serving (170g)

    Cereals ready-to-eat, KELLOGG, KELLOGG'S Corn Flakes
    Group: Breakfast Cereals
    Date: May 8 Meal: Breakfast
    1 cup (1 NLEA serving) (28g)

    Chicken, broilers or fryers, wing, meat and skin, cooked, roasted
    Group: Poultry Products
    Date: May 8 Meal: Lunch
    Refuse: 48 percent Bone
    3 wing, bone removed (102g)

    Ice creams, chocolate, rich
    Group: Sweets
    Date: May 8 Meal: Lunch
    1 cup (148g)

    Carrots, raw
    Group: Vegetables and Vegetable Products
    Date: May 8 Meal: Lunch
    Refuse: 11 percent Crown, tops and scrapings
    1 large (7-1/4" to 8-/1/2" long) (72g)

    Milk, reduced fat, fluid, 2% milkfat, with added vitamin A
    Group: Dairy and Egg Products
    Date: May 8 Meal: Breakfast
    1 cup (244g)

This is of course a bit hard to comprehend. Of course, I wouldn't be bringing this up if there weren't a solution at hand: the `--sort` option. Use it and Pantry will sort the buffer before it is printed. The `--sort` option takes a single argument to indicate how you want your foods sorted. This argument consists of a series of letters, with each letter being the first letter of the trait you wish to use as a sorting key. For example, to sort by `meal` and then by `name`, use `--sort mn`. Lower-case letters sort in ascending order, while upper-case letters sort in descending order. So, to sort the meal names in ascending order and then the food names in descending order, use `--sort mN`:

Example 4.2. Using the `--sort` option

    $ pantry --date "May 8" --sort mN --print traits-blank diary
    Milk, reduced fat, fluid, 2% milkfat, with added vitamin A
    Group: Dairy and Egg Products
    Date: May 8 Meal: Breakfast
    1 cup (244g)

    Cereals ready-to-eat, KELLOGG, KELLOGG'S Corn Flakes
    Group: Breakfast Cereals
    Date: May 8 Meal: Breakfast
    1 cup (1 NLEA serving) (28g)

    Apples, raw, with skin
    Group: Fruits and Fruit Juices
    Date: May 8 Meal: Breakfast
    Refuse: 10 percent Core and stem
    1 large (3-1/4" dia) (223g)

    Snacks, popcorn, oil-popped, unsalted
    Group: Snacks
    Date: May 8 Meal: Dinner
    2 oz (57g)

    McDONALD'S, French Fries
    Group: Fast Foods
    Date: May 8 Meal: Dinner
    1 large serving (170g)

    McDONALD'S, Cheeseburger
    Group: Fast Foods
    Date: May 8 Meal: Dinner
    1 item (119g)

    Ice creams, chocolate, rich
    Group: Sweets
    Date: May 8 Meal: Lunch
    1 cup (148g)

    Chicken, broilers or fryers, wing, meat and skin, cooked, roasted
    Group: Poultry Products
    Date: May 8 Meal: Lunch
    Refuse: 48 percent Bone
    3 wing, bone removed (102g)

    Carrots, raw
    Group: Vegetables and Vegetable Products
    Date: May 8 Meal: Lunch
    Refuse: 11 percent Crown, tops and scrapings
    1 large (7-1/4" to 8-/1/2" long) (72g)

Remember that all traits in Pantry are strings, including the `date` `pctRefuse`, and `qty` traits. These traits are sorted as strings rather than as numbers or as dates. This can lead to unexpected results. For example, sorting the strings `1`, `2`, and `10` will yield `1`, `10`, `2`.

Similarly, when sorting dates, `7-1`, `7-10`, and `7-2` will not yield the results you may expect. An easy fix for this is to use leading zeroes; sorting `07-01`, `07-02`, and `07-10` will do what you expect.

As you can see above, the `meal` traits are sorted in alphabetical order, yielding `Breakfast`, `Dinner`, and `Lunch`. You can, however, sort these into whatever order you wish, such as the more logical `Breakfast`, `Lunch`, and `Dinner`. We'll find out how to do this when we talk about configuring Pantry, later.

Finally, perhaps you want to be able to sort your foods into any arbitrary order. The `order` trait is useful for this purpose. To sort foods into any order, just assign appropriate `order` values for each food (perhaps `a`, `b`, `c` or `010`, `020`, and `030`) and then use `--sort o`. Pantry can even help you by assigning values to the `order` trait automatically, as we will see in the next section.

Prev |  | Next
3.3. Organizing your foods into files | Home | 4.2. More change options