---
title: "Preserve argument ordering in help sections?"  
description: "Preserve argument ordering in help sections?"  
author: "Barbara Jones"  
published: 2014-10-29  
updated: 2014-10-29  
canonical: https://www.mindstick.com/forum/2453/preserve-argument-ordering-in-help-sections  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Preserve argument ordering in help sections?

I have following [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) to [CLI](https://www.mindstick.com/forum/160471/how-to-read-cli-arguments-in-visual-studio-2022)

-sbx

-CSWConfig

-stripInfo

-modelSources

-catchArchive

-swSupplierName

-modelsSWExchnage

but while displaying help it is showing these [options](https://www.mindstick.com/articles/43878/making-the-best-use-of-the-options-trade-ideas) in sorted [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience)(as shown below) which I dont want, I want all the options to be in order as they are added.

-CatchArchive

-CSWConfig

-modelSources

-sbx

-stripInfo

-swSupplierName

```
private void print_help() {    String CONST_STR_CLI_INFO = "ercli.exe custzip";    HelpFormatter formatter = new HelpFormatter();    formatter.setOptionComparator(new Comparator() {         @Override        public int compare(Object o1, Object o2) {            Option op1=(Option) o1;            Option op2=(Option) o2;            return //what to do here?        }    });    formatter.printHelp(CONST_STR_CLI_INFO, null, options, "", true);}
```

## Replies

### Reply by Mark Devid

As the Options() class stores the options in Maps internally, it does not keep any ordering. That means you need to provide your own order as you already found out.

To get the ordering, you can put the keys in a List upfront to have an index of required order for each element:

```
final List<String> optionKeys = new ArrayList<>();        optionKeys.add("sbx");        optionKeys.add("CSWConfig");        optionKeys.add("stripInfo");        optionKeys.add("modelSources");        optionKeys.add("catchArchive");        optionKeys.add("swSupplierName");        optionKeys.add("modelsSWExchnage");
```

\
Then in the Comparator you can compare by index in this list:\

```
@Override    public int compare(Object o1, Object o2) {        Option op1=(Option) o1;        Option op2=(Option) o2;        return Integer.compare(optionKeys.indexOf(op1.getLongOpt()), optionKeys.indexOf(op1.getLongOpt()));    }
```


---

Original Source: https://www.mindstick.com/forum/2453/preserve-argument-ordering-in-help-sections

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
