Position the selected shapes as aligned vertically or horizontally

Can we arrange the shapes to single line horizontally or vertically after one button click.
Before:
image

After:
image

Furthermore, How do I identify whether the nodes are vertically aligned or horizontally aligned?

Use the DrawCommandHandler extension.
For example: DrawCommandHandler | GoJS API

Or just set each node’s position so that they have the same Point.x value. Remember to make all changes within a single transaction.