Order image gallery

Silverstripe Version:
4.0.4
Question:

I have gallery in my page from photos that have Image, title (‘Nazov’), external link (‘LinkImage’) and order number (‘Por’). I want in page show this gallery ordered by order number (‘Por’). Is it possible?

Gallery.php

class Gallery extends DataObject {

    private static $db = array (
		'Por' => 'Text',
        'Nazov' => 'Text',
		'LinkImage' => 'Text',
    );

    private static $has_one = [
        'Fotografia' => Image::class,		
		'Page' => Page::class,
    ];
	
	private static $summary_fields = array (
		'Por' => 'Poradie',
        'GridThumbnail' => '',
        'Nazov' => 'Názov',
		'LinkImage' => 'URL adresa obrázku',
    );
	
	private static $owns = [
        'Fotografia',
		'Page'
    ];
	
	private static $extensions = [
        Versioned::class,
    ];  
	
	private static $versioned_gridfield_extensions = true;

    public function getGridThumbnail() {
        if($this->Fotografia()->exists()) {
            return $this->Fotografia()->Fill(100,100);
        }

        return "(no image)";
    }

    public function getCMSFields() {
        $fields = FieldList::create(
			TextField::create('Por'),
            TextField::create('Nazov'),
			TextField::create('LinkImage'),
            UploadField::create('Fotografia')
        );
        return $fields;
    }
}

Page.php

private static $has_many = [
		'Galeria' => Gallery::class
	];

...

$fields->addFieldToTab('Root.Loga', GridField::create(
            'Galeria',
            'Vloženie loga do článku',
            $this->Galeria(),
            GridFieldConfig_RecordEditor::create()
        ));

Page.ss

<% if $Galeria %> 
				<div class="popup-gallery cms-gallery">	
					<h3 id="galeria">Fotogaléria</h3>
					<% loop $Galeria %>
						<% if $Fotografia %> 							
							<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 inline-block">								
								<div class="hovereffect">
									<a class="photo" href="$Fotografia.Link">
									<img class="img-responsive" src="$Fotografia.Fill(400,400).Link" alt="$Nazov">
										<div class="overlay">
											<i class="fas fa-search-plus"></i>						
										</div>
									</a>
								</div>
							</div>
						<% end_if %>
					<% end_loop %>
				</div>
			<% end_if %>

First of all, I would suggest changing the ordering field to a numeric type, that way sorting should do what you expect:

private static $db = [
    'Por' => 'Int',
    'Nazov' => 'Text',
    'LinkImage' => 'Text'
];

Then you can just set the default sorting for the objects, so wherever they are used, they should be sorted by the ‘Por’ field:

private static $default_sort = 'Por';

Don’t forget to run a dev/build after you’ve made the changes.