Question: I have a code that can make the word reversed word by word, for example, the input is "stack overflow" the output will be "kcats wolfrevo" but the problem is when I input "stack, overflow." the output will be ",kcats .wolfrevo" not "kcats, wolfrevo.".

Is there any way that makes the special character stay in his index? 
I only know java programming basics.

My codes:

```
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a Paragraph: ");
    String str = input.nextLine();

    String[] Words = new String[countWords(str)];
    int temp = str.length()-1;

    for (int j = 0; j < countWords(str); j++) {

        String reverse = "";
        loop:
        for (int i=temp;i>=0;i--) {
            if (str.charAt(i) == ' ') {
                temp=temp-1;
                break;
            }
            temp=temp-1;

            reverse = reverse + str.charAt(i);
        }
        Words[j]=reverse;

    }
    for (int i = countWords(str)-1;i>=0;i--) {
        System.out.print(Words[i]+" ");
    }

}

protected static int countWords(String str) {
    int count = 0;
    if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                count++;
            }
        }
        count = count + 1;
    }
    return count;
}

```

Answer: 
You could create object from titles array and use it for ordering your array of objects.

```js
var objects = [{"title":"title5"},{"title":"title3"},{"title":"title4"},{"title":"title1"},{"title":"title2"}]
var titles = ['title1','title2','title3','title4','title5'].reduce((r, e, i) => Object.assign(r, {[e]: i}), {})

objects.sort((a, b) => titles[a.title] - titles[b.title])
console.log(objects)
```
You could use [\_.indexBy](https://underscorejs.org/#indexBy) to create an `Object` from `objects` variable and then map `titles`:

```
const objectsByTitle = _.indexBy(objects, 'title');
const result = titles.map(it => objectsByTitle[it]);

```

Titles should be unique ofc.